<?php
namespace app\index\controller;

use app\index\model\AccountDiaoModel;
use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\ArchiveDiaoModel;
use app\index\model\ArchiveJiaoListModel;
use app\index\model\ArchiveJiaoModel;
use app\index\model\ArchiveModel;
use app\index\model\ArchiveOutModel;
use app\index\model\ArchiveZhuanModel;
use app\index\model\BidBookModel;
use app\index\model\BidGrantBookModel;
use app\index\model\BidModel;
use app\index\model\BidPoolModel;
use app\index\model\BookJieListModel;
use app\index\model\BookJieModel;
use app\index\model\BookModel;
use app\index\model\CarBaoModel;
use app\index\model\CarJiaModel;
use app\index\model\CarModel;
use app\index\model\CarXiModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertCompanyModel;
use app\index\model\CertDownListModel;
use app\index\model\CertDownModel;
use app\index\model\CertEmpbuModel;
use app\index\model\CertEmployModel;
use app\index\model\CertModel;
use app\index\model\CertPerruModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidListModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseListModel;
use app\index\model\CertUseModel;
use app\index\model\CertZhaoModel;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiJieListModel;
use app\index\model\ContractCaiJieModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractChuJieModel;
use app\index\model\ContractChuListModel;
use app\index\model\ContractChuModel;
use app\index\model\ContractNeiListModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractQuModel;
use app\index\model\ContractShiGongModel;
use app\index\model\ContractShiJieModel;
use app\index\model\ContractShiModel;
use app\index\model\ContractXingModel;
use app\index\model\CossJieListModel;
use app\index\model\CossJieModel;
use app\index\model\CossModel;
use app\index\model\DangJieListModel;
use app\index\model\DangJieModel;
use app\index\model\DangModel;
use app\index\model\DealcomGengModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;
use app\index\model\DealcomZhengModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieListModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceJinModel;
use app\index\model\DeviceLinListModel;
use app\index\model\DeviceLinModel;
use app\index\model\DossJieListModel;
use app\index\model\DossJieModel;
use app\index\model\DossModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudbianListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use app\index\model\EnginChaModel;
use app\index\model\EnginChanListModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginDiaryModel;
use app\index\model\EnginJunjieListModel;
use app\index\model\EnginJunjieModel;
use app\index\model\EnginJunyanModel;
use app\index\model\EnginKuaModel;
use app\index\model\EnginModel;
use app\index\model\EnginShuiModel;
use app\index\model\EnginShuinuoModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginSpeedModel;
use app\index\model\EnginStampListModel;
use app\index\model\EnginStampModel;
use app\index\model\EnginXunListModel;
use app\index\model\EnginXunModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\FlowSetModel;
use app\index\model\HouBaoListModel;
use app\index\model\HouBaoModel;
use app\index\model\HouBaoPiaoModel;
use app\index\model\HouChujinGuanModel;
use app\index\model\HouChujinListModel;
use app\index\model\HouChujinModel;
use app\index\model\HouChushuiListModel;
use app\index\model\HouChushuiModel;
use app\index\model\HouDayshouModel;
use app\index\model\HouDayzhiGuanModel;
use app\index\model\HouDayzhiListModel;
use app\index\model\HouDayzhiModel;
use app\index\model\HouGonghuiListModel;
use app\index\model\HouGonghuiModel;
use app\index\model\HouGongshuiFuModel;
use app\index\model\HouGongshuiGuanModel;
use app\index\model\HouGongshuiListModel;
use app\index\model\HouGongshuiModel;
use app\index\model\HouGongyuModel;
use app\index\model\HouGongzhiGuanModel;
use app\index\model\HouGongzhiListModel;
use app\index\model\HouGongzhiModel;
use app\index\model\HouGongzhiQuModel;
use app\index\model\HouJieListModel;
use app\index\model\HouJieModel;
use app\index\model\HouKouFuwuModel;
use app\index\model\HouKouGongjieModel;
use app\index\model\HouKouKuaifeiModel;
use app\index\model\HouKouQifeiModel;
use app\index\model\HouKouShuifeiModel;
use app\index\model\HouShouModel;
use app\index\model\HouTiaoModel;
use app\index\model\IncomeEnginModel;
use app\index\model\IncomeHuanModel;
use app\index\model\IncomeQiModel;
use app\index\model\IncomeYushouListModel;
use app\index\model\IncomeYushouModel;
use app\index\model\KaoBanModel;
use app\index\model\KaoBaoListModel;
use app\index\model\KaoBaoModel;
use app\index\model\KaochaiModel;
use app\index\model\KaoChuModel;
use app\index\model\KaoJiaModel;
use app\index\model\KaoshiModel;
use app\index\model\KuaiJiModel;
use app\index\model\KuaiShouModel;
use app\index\model\LabourGongModel;
use app\index\model\LabourJieListModel;
use app\index\model\LabourJieModel;
use app\index\model\LabourLingListModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenListModel;
use app\index\model\LabourShenModel;
use app\index\model\LabourYongListModel;
use app\index\model\LabourYongModel;
use app\index\model\MeetModel;
use app\index\model\NoticeModel;
use app\index\model\PaymentBaoListModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentChuModel;
use app\index\model\PaymentDayListModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginListModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouListModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PaymentQianListModel;
use app\index\model\PaymentQianModel;
use app\index\model\PaymentQiListModel;
use app\index\model\PaymentQiModel;
use app\index\model\PaymentYutuiModel;
use app\index\model\PaymentYuzhiListModel;
use app\index\model\PaymentYuzhiModel;
use app\index\model\PengModel;
use app\index\model\PiaoChuListModel;
use app\index\model\PiaoChuModel;
use app\index\model\PiaoDayListModel;
use app\index\model\PiaoDayModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoKouListModel;
use app\index\model\PiaoKouModel;
use app\index\model\PiaoPutListModel;
use app\index\model\PiaoPutModel;
use app\index\model\PiaoPutPayModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\PiaoTaxListModel;
use app\index\model\PiaoTaxModel;
use app\index\model\PortAnModel;
use app\index\model\PortHuiModel;
use app\index\model\PortModel;
use app\index\model\QianBanListModel;
use app\index\model\QianBanModel;
use app\index\model\QianfaModel;
use app\index\model\QianPiaoListModel;
use app\index\model\QianPiaoModel;
use app\index\model\QianShouListModel;
use app\index\model\QianShouModel;
use app\index\model\QianSuoModel;
use app\index\model\QianZhiGuanModel;
use app\index\model\QianZhiListModel;
use app\index\model\QianZhiModel;
use app\index\model\SealGaiListModel;
use app\index\model\SealGaiModel;
use app\index\model\SealModel;
use app\index\model\SealWaiModel;
use app\index\model\SealZhiModel;
use app\index\model\SpeedMonthModel;
use app\index\model\StuffCaiListModel;
use app\index\model\StuffCaiModel;
use app\index\model\StuffJieListModel;
use app\index\model\StuffJieModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\SubbaoJieListModel;
use app\index\model\SubbaoJieModel;
use app\index\model\SubbaoShenListModel;
use app\index\model\SubbaoShenModel;
use app\index\model\TongyongModel;
use app\index\model\TouFanModel;
use app\index\model\TouHanModel;
use app\index\model\TouJiaoListModel;
use app\index\model\TouJiaoModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\VisaModel;
use app\index\model\WageListModel;
use app\index\model\WageModel;
use app\index\model\WagjianListModel;
use app\index\model\WagjianModel;
use app\index\model\WagyuanListModel;
use app\index\model\WagyuanModel;
use app\index\model\WenBanModel;
use app\index\model\WuCaiListModel;
use app\index\model\WuCaiModel;
use app\index\model\WuChuModel;
use app\index\model\WuLingListModel;
use app\index\model\WuLingModel;
use app\index\model\WuRuListModel;
use app\index\model\WuRuModel;
use app\index\model\XossJieListModel;
use app\index\model\XossJieModel;
use app\index\model\XossModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BidService;
use app\index\service\BudgetService;
use app\index\service\CertService;
use app\index\service\ContractService;
use app\index\service\DeviceService;
use app\index\service\EnginService;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\HouService;
use app\index\service\HumanService;
use app\index\service\IncomeService;
use app\index\service\LabourService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\PiaoService;
use app\index\service\QianService;
use app\index\service\SceneService;
use app\index\service\StuffService;
use app\index\service\SubbaoService;
use app\index\service\UserService;
use app\index\service\XingService;
use app\index\validate\CertUse;
use think\Db;
use think\Exception;
use think\facade\Config;
use think\Request;

class Flowdata extends Common
{
    //##############################################  投标  ##############################################
    //##############################################  投标  ##############################################

    public function add_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jinglei'])){
                    return outputJson(4000, '请选择经营类别');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入工程名称');
                }
                if((new BidService())->check_bid_name($param['name'])){
                    return outputJson(4000, '工程名称已存在');
                }
                if(empty($param['jiaer'])){
                    return outputJson(4000, '请输入甲方名称');
                }
                if(empty($param['tou_unit'])){
                    return outputJson(4000, '请选择投标单位');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
                if(empty($param['lanshi'])){
                    return outputJson(4000, '请选择承揽方式');
                }
                if(empty($param['kaitype'])){
                    return outputJson(4000, '请选择开标类型');
                }
                if(empty($param['kailei'])){
                    return outputJson(4000, '请选择工程性质');
                }
                if(empty($param['zhaotype'])){
                    return outputJson(4000, '请选择招标方式');
                }
                if(empty($param['islu'])){
                    return outputJson(4000, '请选择业绩是否录入平台');
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = BidModel::insertGetId($param);

                (new FlowService())->save_bill('bid', $id);

                (new OptService())->relation_file($files,'bid',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid', $id, '工程报备');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
            $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
            $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
            $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
            $data['areas'] = (new OptService())->get_city_cader();
            $data['tou_unit_info'] = (new UserService())->get_users_part_arr(1, 'dept');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('bid');
            $data['flowsetname'] = FlowSetModel::get_name('bid');
            $data['isadvan'] = (new FlowService())->check_isadvan('bid');
            $data['isnext'] = (new FlowService())->check_isnext('bid');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jinglei'])){
                    return outputJson(4000, '请选择经营类别');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入工程名称');
                }
                if((new BidService())->check_bid_name($param['name'], $param['id'])){
                    return outputJson(4000, '工程名称已存在');
                }
                if(empty($param['jiaer'])){
                    return outputJson(4000, '请输入甲方名称');
                }
                if(empty($param['tou_unit'])){
                    return outputJson(4000, '请选择投标单位');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
                if(empty($param['lanshi'])){
                    return outputJson(4000, '请选择承揽方式');
                }
                if(empty($param['kaitype'])){
                    return outputJson(4000, '请选择开标类型');
                }
                if(empty($param['kailei'])){
                    return outputJson(4000, '请选择工程性质');
                }
                if(empty($param['zhaotype'])){
                    return outputJson(4000, '请选择招标方式');
                }
                if(empty($param['islu'])){
                    return outputJson(4000, '请选择业绩是否录入平台');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                BidModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bid', $param['id']);

                }else{
                    (new FlowService())->save_bill('bid', $param['id']);
                }

                (new OptService())->relation_file($files,'bid',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid', $param['id'], '工程报备');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['tou_unit_info'] = (new UserService())->get_users_part_arr($data['data']['tou_unit'], 'dept');
            $data['data']['gui_unit_info'] = (new UserService())->get_users_part_arr($data['data']['gui_unit'], 'dept');
            $data['data']['jinger_info'] = (new UserService())->get_users_part_arr($data['data']['jinger'], 'user');
            $data['data']['touer_info'] = (new UserService())->get_users_part_arr($data['data']['touer'], 'user');


            $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
            $data['zilai'] = (new OptService())->get_base_info('bid_zilai');
            $data['kaitype'] = (new OptService())->get_base_info('bid_kaitype');
            $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
            $data['areas'] = (new OptService())->get_city_cader();


            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('bid');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('bid',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('bid');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_bid_book(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(!isset($param['name']) || empty($param['name'])){
                    return outputJson(4000,'请输入名称');
                }
                if(!isset($param['bidid']) || empty($param['bidid'])){
                    return outputJson(4000,'请选择项目');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $result = BidBookModel::create($param);
                (new FlowService())->save_bill('bid_book', $result->id);
                (new OptService())->relation_file($files,'bid_book',$result->id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_book', $result->id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_book', $result->id, '投标文件审察');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$result->id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('bid_book');
            $data['flowsetname'] = FlowSetModel::get_name('bid_book');
            $data['isadvan'] = (new FlowService())->check_isadvan('bid_book');
            $data['isnext'] = (new FlowService())->check_isnext('bid_book');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['flowdt'] = date('Y-m-d H:i:s');
            $data['maker_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bid_book(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(!isset($param['name']) || empty($param['name'])){
                    return outputJson(4000,'请输入名称');
                }
                if(!isset($param['bidid']) || empty($param['bidid'])){
                    return outputJson(4000,'请选择项目');
                }
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            Db::startTrans();
            try {
                BidBookModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('bid_book', $param['id']);
                }else{
                    (new FlowService())->save_bill('bid_book', $param['id']);
                }
                (new OptService())->relation_file($files,'bid_book',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_book', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_book', $param['id'], '投标文件审察');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                halt($e);
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidBookModel::where(['id'=>$id])->find();
            $bid = BidModel::where('id',$result['bidid'])->find();
            $result['bid_name'] = $bid['name'];

            $result['tou_unit_name'] = DeptModel::get_name_by_pk($bid['tou_unit']);
            $result['enddt_tou'] = $bid['enddt_tou'];
            $result['maker_info'] = (new UserService())->get_users_part_arr($result['maker'], 'user');
            $data['data'] = $result;

            $data['files'] = (new OptService())->get_file('bid_book', $result['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($result['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($result['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($result['nexter'], 'user');
            $data['flowsetname'] = FlowSetModel::get_name('bid_book');
            $data['isadvan'] = (new FlowService())->check_isadvan_edit('bid_book',$result);
            $data['isnext'] = (new FlowService())->check_isnext('bid_book');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    //##############################################  财务  ##############################################
    //##############################################  财务  ##############################################
    public function add_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            $zheng = array_filter($param['zheng']);
            unset($param['zheng']);

            if($param['isturn'] > 0){
                if(count($list) > 0){
                    $isdea = 0;
                    foreach ($list as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '账户信息,请选择默认账户');
                    }
                }

                if(count($zheng) > 0){
                    $isdea = 0;
                    foreach ($zheng as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '证件信息,请选择默认证件');
                    }
                }
            }

            Db::startTrans();
            try {
                $id = DealcomModel::insertGetId($param);


                foreach($list as $vv){
                    $vv['dealcomid'] = $id;
                    DealcomListModel::insert($vv);
                }

                foreach($zheng as $vv){
                    $vv['dealcomid'] = $id;
                    DealcomZhengModel::insert($vv);
                }


                (new FlowService())->save_bill('dealcom', $id);
                (new OptService())->relation_file($files,'dealcom',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $id, '往来单位');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('dealcom');
            $data['flowsetname'] = FlowSetModel::get_name('dealcom');
            $data['isadvan'] = (new FlowService())->check_isadvan('dealcom');
            $data['isnext'] = (new FlowService())->check_isnext('dealcom');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['id'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
                if(empty($param['nature'])){
                    return outputJson(4000, '请选择单位性质');
                }

            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            $zheng = array_filter($param['zheng']);
            unset($param['zheng']);

            if($param['isturn'] > 0){
                if(count($list) > 0){
                    $isdea = 0;
                    foreach ($list as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '账户信息,请选择默认账户');
                    }
                }

                if(count($zheng) > 0){
                    $isdea = 0;
                    foreach ($zheng as $vvv){
                        if($vvv['isdea'] > 0){
                            $isdea = 1;
                        }
                    }
                    if($isdea < 1){
                        return outputJson(4000, '证件信息,请选择默认证件');
                    }
                }
            }


            Db::startTrans();
            try {
                DealcomModel::update($param);

                DealcomListModel::where(['dealcomid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dealcomid'] = $param['id'];
                    DealcomListModel::insert($vv);
                }

                DealcomZhengModel::where(['dealcomid'=>$param['id']])->delete();
                foreach($zheng as $vvv){
                    $vvv['dealcomid'] = $param['id'];
                    DealcomZhengModel::insert($vvv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dealcom', $param['id']);
                }else{
                    (new FlowService())->save_bill('dealcom', $param['id']);
                }

                (new OptService())->relation_file($files,'dealcom',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom', $param['id'], '往来单位');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $list = DealcomListModel::where(['dealcomid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;


            $zheng = DealcomZhengModel::where(['dealcomid'=>$data['data']['id']])->select()->toArray();
            $data['zheng'] = $zheng;

            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dealcom');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dealcom',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dealcom');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_dealcom_geng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['dealid'])){
                    return outputJson(4000, '请选择变更往来单位');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['dealid'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
                if(empty($param['nature'])){
                    return outputJson(4000, '请选择单位性质');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $copyFiles = [];
            foreach ($files as $vvv){
                $arr = FileModel::where(['id'=>$vvv['id']])->find()->toArray();
                unset($arr['id']);
                $copyId = FileModel::insertGetId($arr);
                $copyFiles[] = ['id'=>$copyId];
            }
            $files = $copyFiles;


            Db::startTrans();
            try {
                $id = DealcomGengModel::insertGetId($param);
                (new FlowService())->save_bill('dealcom_geng', $id);
                (new OptService())->relation_file($files,'dealcom_geng',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom_geng', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom_geng', $id, '往来单位变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('dealcom_geng');
            $data['flowsetname'] = FlowSetModel::get_name('dealcom_geng');
            $data['isadvan'] = (new FlowService())->check_isadvan('dealcom_geng');
            $data['isnext'] = (new FlowService())->check_isnext('dealcom_geng');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_dealcom_geng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['dealid'])){
                    return outputJson(4000, '请选择变更往来单位');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入单位名称');
                }
                if((new FinanceService())->check_dealcom_name($param['name'], $param['dealid'])){
                    return outputJson(4000, '单位名称已存在');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择所属分类');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                DealcomGengModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('dealcom_geng', $param['id']);
                }else{
                    (new FlowService())->save_bill('dealcom_geng', $param['id']);
                }

                (new OptService())->relation_file($files,'dealcom_geng',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'dealcom_geng', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'dealcom_geng', $param['id'], '往来单位变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomGengModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $dealid_info = DealcomModel::where(['id'=>$data['data']['dealid']])->find();
            $data['dealid_info'] = $dealid_info;

            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['danlevel'] = (new OptService())->get_base_info('cai_dealcom_danlevel');
            $data['files'] = (new OptService())->get_file('dealcom_geng', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('dealcom_geng');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('dealcom_geng',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('dealcom_geng');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_kua(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择发包方(购买方)');
                }
                if(empty($param['ratenum'])){
                    return outputJson(4000, '请输入纳税人识别号');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入本次开具跨区域涉税金额');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = EnginKuaModel::insertGetId($param);

                (new FlowService())->save_bill('engin_kua', $id);

                (new OptService())->relation_file($files,'engin_kua',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_kua', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_kua', $id, '跨区域涉税事项');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);
            if($data['engininfo']){
                $data['engininfo']['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['name'=>$data['engininfo']['gofuer']])->value('liantel');

                $data['engininfo']['neiinfo'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['engininfo']['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->find();

                $data['engininfo']['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['engininfo']['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->find();
                if($data['engininfo']['shiinfo']){
                    $data['engininfo']['shiinfo']['chenger_name'] = DeptModel::get_name_by_pk($data['engininfo']['shiinfo']['chenger']);
                    $data['engininfo']['shiinfo']['faerinfo'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$data['engininfo']['shiinfo']['faer'],'isturn'=>1,'status'=>1])->find();
                }

                $data['engininfo']['shijiefee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['engininfo']['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->sum('money');

            }


            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('engin_kua');
            $data['flowsetname'] = FlowSetModel::get_name('engin_kua');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_kua');
            $data['isnext'] = (new FlowService())->check_isnext('engin_kua');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_kua(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择发包方(购买方)');
                }
                if(empty($param['ratenum'])){
                    return outputJson(4000, '请输入纳税人识别号');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入本次开具跨区域涉税金额');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                EnginKuaModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_kua', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_kua', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_kua',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_kua', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_kua', $param['id'], '跨区域涉税事项');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginKuaModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['files'] = (new OptService())->get_file('engin_kua', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_kua');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_kua',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_kua');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['kaier'])){
                    return outputJson(4000, '请选择开票单位');
                }
                if(empty($param['kai_zhangname'])){
                    return outputJson(4000, '请选择销售方账户名称');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择发包方(购买方)');
                }

                if(empty($param['money'])){
                    return outputJson(4000, '请输入开票金额');
                }
                if(empty($param['ishong'])){
                    return outputJson(4000, '请选择是否红冲');
                }
                if(empty($param['qianpiaotype'])){
                    return outputJson(4000, '请选择发票类型');
                }
                if(empty($param['shuilv'])){
                    return outputJson(4000, '请选择税率');
                }
                if(empty($param['kuantype'])){
                    return outputJson(4000, '请选择票款方式');
                }
                if(empty($param['ratenum'])){
                    return outputJson(4000, '请输入纳税人识别号');
                }



                if($param['ishong'] != '是'){
                    if($param['isshou'] == '是'){
                        if($param['shuishou'] == 1){
                            if(empty($param['kouids'])){
                                return outputJson(4000, '请选择关联收取');
                            }
                            $param['shuiid'] = 0;
                        }else{
                            if(empty($param['shuiid'])){
                                return outputJson(4000, '请选择税款承诺书');
                            }
                            $param['kouids'] = '';
                        }
                    }
                    if(empty($param['iskaishe'])){
                        return outputJson(4000, '请选择开具跨区域涉税事项表');
                    }
                    if($param['iskaishe'] == '是'){
                        if(empty($param['waijiao'])){
                            return outputJson(4000, '请选择外管税缴纳方式');
                        }
                    }
                }

            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            if($param['ishong'] == '是'){
                if($param['money'] > 0){
                    $param['money'] = -$param['money'];
                }
            }


            Db::startTrans();
            try {
                $id = PiaokaiModel::insertGetId($param);

                (new FlowService())->save_bill('piao_kai', $id);

                (new OptService())->relation_file($files,'piao_kai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_kai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_kai', $id, '开票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);
            if($data['engininfo']){
                $data['engininfo']['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['name'=>$data['engininfo']['gofuer']])->value('liantel');

                $data['engininfo']['neiinfo'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['engininfo']['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->find();


                $data['engininfo']['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['engininfo']['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->find();
                if($data['engininfo']['shiinfo']){
                    $data['engininfo']['shiinfo']['chenger_name'] = DeptModel::get_name_by_pk($data['engininfo']['shiinfo']['chenger']);
                    $data['engininfo']['shiinfo']['faerinfo'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'name'=>$data['engininfo']['shiinfo']['faer'],'isturn'=>1,'status'=>1])->find();
                }


                $data['engininfo']['shijiefee'] = ContractShiJieModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['engininfo']['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->sum('money');


                $data['engininfo']['shangpiaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['engininfo']['id'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1])->order('id DESC')->find();
                if($data['engininfo']['shangpiaokai']){
                    $data['engininfo']['shangpiaokai']['kaier_info'] = (new UserService())->get_users_part_arr($data['engininfo']['shangpiaokai']['kaier'], 'dept');
                }
            }


            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['shuilv'] = (new OptService())->get_base_info('cai_piao_shuilv');
            $data['kuantype'] = (new OptService())->get_base_info('cai_kuantype');
            $data['funei'] = (new OptService())->get_base_info('cai_piao_funei');
            $data['waijiao'] = (new PiaoService())->get_kai_waijiao();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('piao_kai');
            $data['flowsetname'] = FlowSetModel::get_name('piao_kai');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_kai');
            $data['isnext'] = (new FlowService())->check_isnext('piao_kai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_kai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['kaier'])){
                    return outputJson(4000, '请选择开票单位');
                }
                if(empty($param['kai_zhangname'])){
                    return outputJson(4000, '请选择销售方账户名称');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择发包方(购买方)');
                }

                if(empty($param['money'])){
                    return outputJson(4000, '请输入开票金额');
                }
                if(empty($param['ishong'])){
                    return outputJson(4000, '请选择是否红冲');
                }
                if(empty($param['qianpiaotype'])){
                    return outputJson(4000, '请选择发票类型');
                }
                if(empty($param['shuilv'])){
                    return outputJson(4000, '请选择税率');
                }
                if(empty($param['kuantype'])){
                    return outputJson(4000, '请选择票款方式');
                }
                if(empty($param['ratenum'])){
                    return outputJson(4000, '请输入纳税人识别号');
                }



                if($param['ishong'] != '是'){
                    if($param['isshou'] == '是'){
                        if($param['shuishou'] == 1){
                            if(empty($param['kouids'])){
                                return outputJson(4000, '请选择关联收取');
                            }
                            $param['shuiid'] = 0;
                        }else{
                            if(empty($param['shuiid'])){
                                return outputJson(4000, '请选择税款承诺书');
                            }
                            $param['kouids'] = '';
                        }
                    }
                    if(empty($param['iskaishe'])){
                        return outputJson(4000, '请选择开具跨区域涉税事项表');
                    }
                    if($param['iskaishe'] == '是'){
                        if(empty($param['waijiao'])){
                            return outputJson(4000, '请选择外管税缴纳方式');
                        }
                    }
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            if($param['ishong'] == '是'){
                if($param['money'] > 0){
                    $param['money'] = -$param['money'];
                }
            }


            Db::startTrans();
            try {
                PiaokaiModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_kai', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_kai', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_kai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_kai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_kai', $param['id'], '开票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaokaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['kaier_info'] = (new UserService())->get_users_part_arr($data['data']['kaier'], 'dept');

            $data['data']['shuishou_name'] = $data['data']['shuishou'] == 1 ? '开票已预缴' : '回款待收取';

            if($data['data']['shuishou'] == 1){
                $data['data']['kouids_name'] = '';
                $kouidsArr = HouKouQifeiModel::where('id','in',$data['data']['kouids'])->select()->toArray();
                foreach ($kouidsArr as $kkvv){
                    if($kkvv['type'] == 1){
                        $billid = HouGonghuiModel::where(['id'=>$kkvv['zhuid']])->value('billid');
                    }else{
                        $billid = HouGongyuModel::where(['id'=>$kkvv['zhuid']])->value('billid');
                    }
                    $sericnum = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$billid])->value('sericnum');
                    if($data['data']['kouids_name']){
                        $data['data']['kouids_name'] .= '，'.$sericnum;
                    }else{
                        $data['data']['kouids_name'] .= $sericnum;
                    }
                }
            }else{
                $shuiidinfo = EnginShuiModel::where(['id'=>$data['data']['shuiid']])->find();
                $data['shuiidinfo'] = $shuiidinfo;
            }



            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['shuilv'] = (new OptService())->get_base_info('cai_piao_shuilv');
            $data['kuantype'] = (new OptService())->get_base_info('cai_kuantype');
            $data['funei'] = (new OptService())->get_base_info('cai_piao_funei');
            $data['waijiao'] = (new PiaoService())->get_kai_waijiao();

            $data['files'] = (new OptService())->get_file('piao_kai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_kai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_kai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_kai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'zhikuanlei') !== false){
                    unset($param[$k]);
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }


                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoShouListModel::alias('list')
                        ->join('piao_shou piao','piao.id=list.shouid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                $id = PiaoShouModel::insertGetId($param);


                foreach($list as $vv){
                    $vv['shouid'] = $id;
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = PiaoShouListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouGongzhiGuanModel::insert([
                            'type' => 2,
                            'gongzhiid' => $vvvv['gongzhiid'],
                            'gongzhilistid' => $vvvv['gongzhilistid'],
                            'piaoid' => $id,
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }




                (new FlowService())->save_bill('piao_shou', $id);

                (new OptService())->relation_file($files,'piao_shou',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_shou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_shou', $id, '进项票登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);


            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('piao_shou');
            $data['flowsetname'] = FlowSetModel::get_name('piao_shou');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_shou');
            $data['isnext'] = (new FlowService())->check_isnext('piao_shou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'zhikuanlei') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoShouListModel::alias('list')
                        ->join('piao_shou piao','piao.id=list.shouid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where('piao.id','neq',$param['id'])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                PiaoShouModel::update($param);

                PiaoShouListModel::where(['shouid'=>$param['id']])->delete();
                HouGongzhiGuanModel::where(['piaoid'=>$param['id'],'type'=>2])->delete();
                foreach($list as $vv){
                    $vv['shouid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = PiaoShouListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouGongzhiGuanModel::insert([
                            'type' => 2,
                            'gongzhiid' => $vvvv['gongzhiid'],
                            'gongzhilistid' => $vvvv['gongzhilistid'],
                            'piaoid' => $param['id'],
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }




                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_shou', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_shou', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_shou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_shou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_shou', $param['id'], '进项票登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $list = PiaoShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['caiid_name'] = '';
                $caiidInfo = ContractCaiModel::where(['id'=>$item['caiid']])->find();
                if($caiidInfo){
                    $item['caiid_name'] = $caiidInfo['name'];
                }

                $difeeList = HouGongzhiGuanModel::field('gongzhiid,gongzhilistid,difee')
                    ->where(['type'=>2,'piaoid'=>$item['shouid'],'piaolistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;

            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
            $data['files'] = (new OptService())->get_file('piao_shou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_shou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_shou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_shou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_piao_day(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入事项名称');
                }
                if(empty($param['kuantype'])){
                    return outputJson(4000, '请选择票款方式');
                }
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoDayListModel::alias('list')
                        ->join('piao_day piao','piao.id=list.dayid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                $id = PiaoDayModel::insertGetId($param);


                foreach($list as $vv){
                    $vv['dayid'] = $id;
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = PiaoDayListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouDayzhiGuanModel::insert([
                            'type' => 2,
                            'dayzhiid' => $vvvv['dayzhiid'],
                            'dayzhilistid' => $vvvv['dayzhilistid'],
                            'piaoid' => $id,
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }

                (new FlowService())->save_bill('piao_day', $id);
                (new OptService())->relation_file($files,'piao_day',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_day', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_day', $id, '日常进项票登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['kuantype'] = (new OptService())->get_base_info('cai_kuantype');
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('piao_day');
            $data['flowsetname'] = FlowSetModel::get_name('piao_day');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_day');
            $data['isnext'] = (new FlowService())->check_isnext('piao_day');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_day(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入事项名称');
                }
                if(empty($param['kuantype'])){
                    return outputJson(4000, '请选择票款方式');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }


                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoDayListModel::alias('list')
                        ->join('piao_day piao','piao.id=list.dayid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where('piao.id','neq',$param['id'])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                PiaoDayModel::update($param);

                PiaoDayListModel::where(['dayid'=>$param['id']])->delete();
                HouDayzhiGuanModel::where(['piaoid'=>$param['id'],'type'=>2])->delete();
                foreach($list as $vv){
                    $vv['dayid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = PiaoDayListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouDayzhiGuanModel::insert([
                            'type' => 2,
                            'dayzhiid' => $vvvv['dayzhiid'],
                            'dayzhilistid' => $vvvv['dayzhilistid'],
                            'piaoid' => $param['id'],
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }




                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_day', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_day', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_day',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_day', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_day', $param['id'], '日常进项票登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoDayModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = PiaoDayListModel::where(['dayid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $difeeList = HouDayzhiGuanModel::field('dayzhiid,dayzhilistid,difee')
                    ->where(['type'=>2,'piaoid'=>$item['dayid'],'piaolistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;

            $data['kuantype'] = (new OptService())->get_base_info('cai_kuantype');
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
            $data['files'] = (new OptService())->get_file('piao_day', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_day');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_day',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_day');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_piao_put(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }


            $param['number'] = (new PiaoService())->create_piao_put_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            $painfo = array_filter($param['painfo']);
            unset($param['painfo']);



            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }
            }

            if(count($painfo) > 0){
                $param['ispay'] = 1;
            }else{
                $param['ispay'] = 0;
            }

            Db::startTrans();
            try {
                $id = PiaoPutModel::insertGetId($param);

                foreach ($list as $item) {
                    $item['putid'] = $id;
                    PiaoPutListModel::insert($item);
                }

                foreach ($painfo as $vvv) {
                    $vvv['putid'] = $id;
                    PiaoPutPayModel::insert($vvv);
                }

                (new FlowService())->save_bill('piao_put', $id);

                (new OptService())->relation_file($files,'piao_put',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_put', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_put', $id, '收票登记申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['piaotype'] = (new PiaoService())->get_put_piaotype();
            $data['number'] = (new PiaoService())->create_piao_put_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('piao_put');
            $data['flowsetname'] = FlowSetModel::get_name('piao_put');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_put');
            $data['isnext'] = (new FlowService())->check_isnext('piao_put');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_put(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            $painfo = array_filter($param['painfo']);
            unset($param['painfo']);



            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }
            }

            if(count($painfo) > 0){
                $param['ispay'] = 1;
            }else{
                $param['ispay'] = 0;
            }

            Db::startTrans();
            try {
                PiaoPutModel::update($param);

                PiaoPutListModel::where(['putid'=>$param['id']])->delete();
                foreach ($list as $item) {
                    $item['putid'] = $param['id'];
                    PiaoPutListModel::insert($item);
                }

                PiaoPutPayModel::where(['putid'=>$param['id']])->delete();
                foreach ($painfo as $vvv) {
                    $vvv['putid'] = $param['id'];
                    PiaoPutPayModel::insert($vvv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_put', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_put', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_put',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_put', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_put', $param['id'], '收票登记申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoPutModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['shimoney'] = 0;
            if($engininfo){
                $engininfo['shiname'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
                    ->where('status','in',[0,1])->value('name');
                $data['engininfo'] = $engininfo;
                $data['shimoney'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1])
                    ->where('status','in',[0,1])->value('money');
            }

            $list = PiaoPutListModel::where(['putid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['piaotype_name'] = (new PiaoService())->get_put_piaotype_name($item['piaotype']);
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;

            $payinfo = PiaoPutPayModel::where(['putid'=>$data['data']['id']])->select()->toArray();
            foreach ($payinfo as &$item){
                $pay = PaymentEnginModel::where(['id'=>$item['payenid']])->find();

                $item['number'] = $pay['number'];
                $item['gonger'] = $pay['gonger'];
                $item['money'] = $pay['money'];
                $item['payfee'] = $pay['payfee'];
                $item['qingdt'] = date("Y-m-d", strtotime($pay['created_at']));
                $item['paydt'] = AccountListModel::where(['type'=>3,'mid'=>$pay['id']])->order('dt DESC')->value('dt');
                $item['paystatus'] = (new PaymentService())->get_paydata_paystatus_name($pay['money'], $pay['payfee']);
            }
            $data['payinfo'] = $payinfo;


            $data['piaotype'] = (new PiaoService())->get_put_piaotype();
            $data['files'] = (new OptService())->get_file('piao_put', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_put');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_put',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_put');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function edit_hou_chujin22(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }



            Db::startTrans();
            try {
                HouChujinModel::update($param);

                HouChujinListModel::where(['chujinid'=>$param['id']])->delete();
                HouChujinGuanModel::where(['chujinid'=>$param['id'],'type'=>1])->delete();
                foreach($list as $vv){
                    $vv['chujinid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = HouChujinListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouChujinGuanModel::insert([
                            'type' => 1,
                            'chujinid' => $param['id'],
                            'chujinlistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }





                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_chujin', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_chujin', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_chujin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_chujin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_chujin', $param['id'], '储备进项支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouChujinModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['liantel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'lianer'=>$data['data']['lianer']])->value('liantel');

            $list = HouChujinListModel::where(['chujinid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['chuid_name'] = '';
                $chuInfo = ContractChuModel::where(['id'=>$item['chuid']])->find();
                if($chuInfo){
                    $item['chuid_name'] = $chuInfo['name'];
                    $item['lei_kuan'] = HouChujinListModel::alias('list')
                        ->join('hou_chujin hou','hou.id=list.chujinid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.chuid'=>$item['chuid']])
                        ->sum('list.money');
                    $item['lei_piao'] = PiaoChuListModel::alias('list')
                        ->join('piao_chu hou','hou.id=list.piaochuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.chuid'=>$item['chuid']])
                        ->sum('list.money');
                    $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                        ->where(['isturn'=>1,'status'=>1,'id'=>$item['chuid']])
                        ->sum('money');
                }else{
                    $item['lei_kuan'] = HouChujinListModel::alias('list')
                        ->join('hou_chujin hou','hou.id=list.chujinid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.fuer'=>$item['fuer']])
                        ->sum('list.money');
                    $item['lei_piao'] = PiaoChuListModel::alias('list')
                        ->join('piao_chu hou','hou.id=list.piaochuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.xiaoer'=>$item['fuer']])
                        ->sum('list.money');
                    $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                        ->where(['isturn'=>1,'status'=>1,'gonger'=>$item['fuer']])
                        ->sum('money');
                }

                $difeeList = HouChujinGuanModel::field('piaoid,piaolistid,difee')
                    ->where(['type'=>1,'chujinid'=>$item['chujinid'],'chujinlistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);

            }
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('hou_chujin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_chujin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_chujin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_chujin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function add_piao_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }


                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoChuListModel::alias('list')
                        ->join('piao_chu piao','piao.id=list.piaochuid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                $id = PiaoChuModel::insertGetId($param);

                foreach ($list as $item) {
                    $item['piaochuid'] = $id;
                    $difeeobj = json_decode($item['difeeobj'], true) ?: [];
                    unset($item['difeeobj']);
                    $listid = PiaoChuListModel::insertGetId($item);

                    foreach($difeeobj as $vvvv){
                        HouChujinGuanModel::insert([
                            'type' => 2,
                            'chujinid' => $vvvv['chujinid'],
                            'chujinlistid' => $vvvv['chujinlistid'],
                            'piaoid' => $id,
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }


                (new FlowService())->save_bill('piao_chu', $id);

                (new OptService())->relation_file($files,'piao_chu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'piao_chu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'piao_chu', $id, '储备进项票登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('piao_chu');
            $data['flowsetname'] = FlowSetModel::get_name('piao_chu');
            $data['isadvan'] = (new FlowService())->check_isadvan('piao_chu');
            $data['isnext'] = (new FlowService())->check_isnext('piao_chu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_piao_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入发票明细');
                }


                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = PiaoChuListModel::alias('list')
                        ->join('piao_chu piao','piao.id=list.piaochuid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where('piao.id','neq',$param['id'])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }



            Db::startTrans();
            try {
                PiaoChuModel::update($param);

                PiaoChuListModel::where(['piaochuid'=>$param['id']])->delete();
                HouChujinGuanModel::where(['piaoid'=>$param['id'],'type'=>2])->delete();
                foreach ($list as $item) {
                    $item['piaochuid'] = $param['id'];
                    $difeeobj = json_decode($item['difeeobj'], true) ?: [];
                    unset($item['difeeobj']);
                    $listid = PiaoChuListModel::insertGetId($item);

                    foreach($difeeobj as $vvvv){
                        HouChujinGuanModel::insert([
                            'type' => 2,
                            'chujinid' => $vvvv['chujinid'],
                            'chujinlistid' => $vvvv['chujinlistid'],
                            'piaoid' => $param['id'],
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }



                if($isshen > 0){
                    (new FlowService())->save_bill_shen('piao_chu', $param['id']);
                }else{
                    (new FlowService())->save_bill('piao_chu', $param['id']);
                }

                (new OptService())->relation_file($files,'piao_chu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'piao_chu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'piao_chu', $param['id'], '储备进项票登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PiaoChuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['liantel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'lianer'=>$data['data']['lianer']])->value('liantel');


            $list = PiaoChuListModel::where(['piaochuid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['chuid_name'] = ContractChuModel::where(['id'=>$item['chuid']])->value('name');

                $difeeList = HouChujinGuanModel::field('chujinid,chujinlistid,difee')
                    ->where(['type'=>2,'piaoid'=>$item['piaochuid'],'piaolistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;

            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');

            $data['files'] = (new OptService())->get_file('piao_chu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('piao_chu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('piao_chu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('piao_chu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }




    //##############################################  证书  ##############################################
    //##############################################  证书  ##############################################
    public function add_cert_use(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }

                $exist = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['enginid'=>$param['enginid']])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $useid = CertUseModel::insertGetId($param);

                foreach ($list as $item){
                    $item['useid'] = $useid;
                    CertUseListModel::insert($item);
                }

                (new FlowService())->save_bill('cert_use', $useid);

                (new OptService())->relation_file($files,'cert_use',$useid);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_use', $useid);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_use', $useid, '报建用证');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$useid]);
        }else{
            $data['place'] = CertModel::get_place();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('cert_use');
            $data['flowsetname'] = FlowSetModel::get_name('cert_use');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_use');
            $data['isnext'] = (new FlowService())->check_isnext('cert_use');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_use(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                $exist = CertUseModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['enginid'=>$param['enginid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }



            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');


            Db::startTrans();
            try {
                CertUseModel::update($param);

                CertUseListModel::where('useid',$param['id'])->delete();
                foreach ($list as $item){
                    $item['useid'] = $param['id'];
                    CertUseListModel::insert($item);
                }




                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_use', $param['id']);

                }else{
                    (new FlowService())->save_bill('cert_use', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_use',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_use', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_use', $param['id'], '用证申请');
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertUseModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
            $data['engininfo'] = $engininfo;

            $list = CertUseListModel::where(['useid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $certInfo = CertModel::where(['id'=>$item['certid']])->find();
                $item['name'] = $certInfo['name'];
                $item['number'] = $certInfo['number'];
                $item['major_name'] = str_replace('[]', ',', $certInfo['major']);
                $item['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
                $item['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                $item['opt_name'] = $item['opt'] > 0 ? '在用' : '停用';
            }
            $data['list'] = $list;

            $data['place'] = CertModel::get_place();

            $data['files'] = (new OptService())->get_file('cert_use', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_use');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_use',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_use');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_cert_employ(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                $check = $this->validate($param,'CertEmploy.add');
                if($check !== true){
                    return outputJson(4000,$check);
                }

            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = CertEmployModel::insertGetId($param);


                (new OptService())->relation_file($files,'cert_employ',$id);
                (new FlowService())->save_bill('cert_employ', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_employ', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_employ', $id, '聘费合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $personid = $request->get('personid', 0, 'int');
            $person = CertPersonModel::where(['id'=>$personid])->find();
            $data['person'] = $person;

            $data['channel'] = (new OptService())->get_base_info('cert_employ_channel');
            $data['qualification'] = (new OptService())->get_base_info('cert_employ_qualification');
            $data['purpose'] = (new OptService())->get_base_info('cert_employ_purpose');
            $data['pay_type'] = CertEmployModel::get_pay_type();
            $data['iszhu'] = CertEmployModel::get_iszhu();
            $data['person_nature'] = (new OptService())->get_base_info('person_nature');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('cert_employ');
            $data['flowsetname'] = FlowSetModel::get_name('cert_employ');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_employ');
            $data['isnext'] = (new FlowService())->check_isnext('cert_employ');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_employ(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                $check = $this->validate($param,'CertEmploy.edit');
                if($check !== true){
                    return outputJson(4000,$check);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                CertEmployModel::update($param);


                (new OptService())->relation_file($files,'cert_employ',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_employ', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_employ', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_employ', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_employ', $param['id'], '聘费合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertEmployModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['person_name'] = CertPersonModel::where('id',$data['data']['personid'])->value('name');
            $data['data']['qualification'] = explode(',',$data['data']['qualification']);

            $data['channel'] = (new OptService())->get_base_info('cert_employ_channel');
            $data['qualification'] = (new OptService())->get_base_info('cert_employ_qualification');
            $data['purpose'] = (new OptService())->get_base_info('cert_employ_purpose');
            $data['pay_type'] = CertEmployModel::get_pay_type();
            $data['iszhu'] = CertEmployModel::get_iszhu();
            $data['person_nature'] = (new OptService())->get_base_info('person_nature');

            $data['files'] = (new OptService())->get_file('cert_employ', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_employ');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_employ',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_employ');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_use_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }

                $exist = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['bidid'=>$param['bidid']])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $useid = CertUseBidModel::insertGetId($param);

                foreach ($list as $item){
                    $item['useid'] = $useid;
                    CertUseBidListModel::insert($item);
                }

                (new FlowService())->save_bill('cert_use_bid', $useid);

                (new OptService())->relation_file($files,'cert_use_bid',$useid);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_use_bid', $useid);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_use_bid', $useid, '投标用证');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$useid]);
        }else{
            $bidid = $request->get('bidid',0,'int');
            $ids = $request->get('ids',0,'string');

            $engininfo = BidModel::where(['id'=>$bidid])->find();
            $data['engininfo'] = $engininfo;


            $list = [];
            if($engininfo){
                $existCertid = [];
                $certuse = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'isreal'=>1,'bidid'=>$engininfo['id']])->find();
                if($certuse){
                    $certuselist = CertUseBidListModel::where(['useid'=>$certuse['id']])->select()->toArray();
                    foreach ($certuselist as $vvv){
                        $existCertid[] = $vvv['certid'];
                        $certInfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        $vvv['name'] = $certInfo['name'];
                        $vvv['number'] = $certInfo['number'];
                        $vvv['major_name'] = str_replace('[]', ',', $certInfo['major']);
                        $vvv['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
                        $vvv['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
                        $vvv['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                        $list[] = $vvv;
                    }
                }

                $certList = CertModel::where(['comid'=>$this->userinfo['comid']])->where('id','in',$ids)->select()->toArray();
                foreach ($certList as $item){
                    if(!in_array($item['id'], $existCertid)){
                        $item['certid'] = $item['id'];
                        $item['major_name'] = str_replace('[]', ',', $item['major']);
                        $item['person_name'] = CertPersonModel::where(['id'=>$item['personid']])->value('name');
                        $item['statueuse_name'] = (new CertService())->get_cert_statueuse_name($item['statueuse']);
                        $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                        $item['opt'] = 1;
                        $list[] = $item;
                    }
                }
            }

            $data['list'] = $list;



            $data['place'] = CertModel::get_place();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('cert_use_bid');
            $data['flowsetname'] = FlowSetModel::get_name('cert_use_bid');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_use_bid');
            $data['isnext'] = (new FlowService())->check_isnext('cert_use_bid');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_use_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                $exist = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>0])
                    ->where(['bidid'=>$param['bidid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目已有用证申请正在审核中，无法提交');
                }

                if($param['isturn'] > 0){
                    if(count($list) < 1){
                        return outputJson(4000, '请输入用证明细');
                    }
                }
            }



            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            $isshen = $param['isshen'];
            unset($param['isshen']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');


            Db::startTrans();
            try {
                CertUseBidModel::update($param);

                CertUseBidListModel::where('useid',$param['id'])->delete();
                foreach ($list as $item){
                    $item['useid'] = $param['id'];
                    CertUseBidListModel::insert($item);
                }




                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_use_bid', $param['id']);

                }else{
                    (new FlowService())->save_bill('cert_use_bid', $param['id']);
                }

                (new OptService())->relation_file($files,'cert_use_bid',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_use_bid', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_use_bid', $param['id'], '投标申请');
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertUseBidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = BidModel::where(['id'=>$data['data']['bidid']])->find();
            $data['engininfo'] = $engininfo;

            $list = CertUseBidListModel::where(['useid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $certInfo = CertModel::where(['id'=>$item['certid']])->find();
                $item['name'] = $certInfo['name'];
                $item['number'] = $certInfo['number'];
                $item['major_name'] = str_replace('[]', ',', $certInfo['major']);
                $item['person_name'] = CertPersonModel::where(['id'=>$certInfo['personid']])->value('name');
                $item['statueuse_name'] = (new CertService())->get_cert_statueuse_name($certInfo['statueuse']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($certInfo['chanid']);
                $item['opt_name'] = $item['opt'] > 0 ? '在用' : '停用';
            }
            $data['list'] = $list;

            $data['place'] = CertModel::get_place();

            $data['files'] = (new OptService())->get_file('cert_use_bid', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_use_bid');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_use_bid',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_use_bid');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_borrow(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['lingtype'])){
                    return outputJson(4000, '请选择领取方式');
                }

            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>1,'list.certid'=>$vvv['certid']])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>2,'list.certid'=>$vvv['certid']])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                $id = CertBorrowModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['mid'] = $id;
                    CertBorrowListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'cert_borrow',$id);
                (new FlowService())->save_bill('cert_borrow', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_borrow', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_borrow', $id, '借证申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['lingtype'] = (new CertService())->get_cert_borrow_lingtype();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('cert_borrow');
            $data['flowsetname'] = FlowSetModel::get_name('cert_borrow');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_borrow');
            $data['isnext'] = (new FlowService())->check_isnext('cert_borrow');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_borrow(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['lingtype'])){
                    return outputJson(4000, '请选择领取方式');
                }

            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>1,'list.certid'=>$vvv['certid']])
                            ->where('bw.id','neq',$param['id'])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                        if($cerinfo['goes'] != 1){
                            return outputJson(4000, "第".$kknum."行证书去向不是在册，无法借出");
                        }
                        if($cerinfo['form'] != 1){
                            return outputJson(4000, "第".$kknum."行证书形式不是纸质原件，无法借出");
                        }
                        $exist = CertBorrowListModel::alias('list')
                            ->join('cert_borrow bw','bw.id=list.mid')
                            ->where(['bw.comid'=>$this->userinfo['comid'],'bw.deleted_user'=>0])
                            ->where(['bw.isturn'=>1,'bw.status'=>0])
                            ->where(['list.type'=>2,'list.certid'=>$vvv['certid']])
                            ->where('bw.id','neq',$param['id'])
                            ->find();
                        if($exist){
                            return outputJson(4000, "第".$kknum."行证书已在借出申请中，无法借出");
                        }
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                CertBorrowModel::update($param);

                CertBorrowListModel::where(['mid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['mid'] = $param['id'];
                    CertBorrowListModel::insert($vv);
                }


                (new OptService())->relation_file($files,'cert_borrow',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_borrow', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_borrow', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_borrow', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_borrow', $param['id'], '借证申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertBorrowModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = CertBorrowListModel::where(['mid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                if($item['type'] == 1){
                    $certinfo = CertModel::where(['id'=>$item['certid']])->find();
                    $person = CertPersonModel::where(['id'=>$certinfo['personid']])->find();
                    $item['major'] = str_replace('[]','，',$certinfo['major']);
                    $item['major_name'] = str_replace('[]','，',$certinfo['major']);
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$person['name'];
                }
                if($item['type'] == 2){
                    $certinfo = CertCompanyModel::where(['id'=>$item['certid']])->find();
                    $item['major'] = '';
                    $item['major_name'] = '';
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_company_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_company_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$item['chanid_name'];
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('cert_borrow', $data['data']['id']);
            $data['lingtype'] = (new CertService())->get_cert_borrow_lingtype();
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_borrow');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_borrow',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_borrow');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_cert_down(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                $id = CertDownModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['mid'] = $id;
                    CertDownListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'cert_down',$id);
                (new FlowService())->save_bill('cert_down', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'cert_down', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'cert_down', $id, '证件下载申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('cert_down');
            $data['flowsetname'] = FlowSetModel::get_name('cert_down');
            $data['isadvan'] = (new FlowService())->check_isadvan('cert_down');
            $data['isnext'] = (new FlowService())->check_isnext('cert_down');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_cert_down(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入证件明细');
                }


                $kknum = 1;
                foreach ($list as $vvv){
                    if($vvv['type'] == 1){
                        $cerinfo = CertModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }

                    if($vvv['type'] == 2){
                        $cerinfo = CertCompanyModel::where(['id'=>$vvv['certid']])->find();
                        if($cerinfo['state'] != 1){
                            return outputJson(4000, "第".$kknum."行证书状态不是正常，无法借出");
                        }
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                CertDownModel::update($param);

                CertDownListModel::where(['mid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['mid'] = $param['id'];
                    CertDownListModel::insert($vv);
                }


                (new OptService())->relation_file($files,'cert_down',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('cert_down', $param['id']);
                }else{
                    (new FlowService())->save_bill('cert_down', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'cert_down', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'cert_down', $param['id'], '证件下载申请');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CertDownModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = CertDownListModel::where(['mid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                if($item['type'] == 1){
                    $certinfo = CertModel::where(['id'=>$item['certid']])->find();
                    $person = CertPersonModel::where(['id'=>$certinfo['personid']])->find();
                    $item['major'] = str_replace('[]','，',$certinfo['major']);
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$person['name'];
                }
                if($item['type'] == 2){
                    $certinfo = CertCompanyModel::where(['id'=>$item['certid']])->find();
                    $item['major'] = '';
                    $item['number'] = $certinfo['number'];
                    $item['form_name'] = (new CertService())->get_cert_company_form_name($certinfo['form']);
                    $item['goes_name'] = (new CertService())->get_cert_company_goes_name($certinfo['goes']);
                    $item['chanid_name'] = DeptModel::get_name_by_pk($certinfo['chanid']);
                    $item['name'] = $certinfo['name'].'-'.$item['chanid_name'];
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('cert_down', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('cert_down');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('cert_down',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('cert_down');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }




    //##############################################  项目  ##############################################
    //##############################################  项目  ##############################################
    public function add_engin_shui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入名称');
                }
                if(empty($param['chener'])){
                    return outputJson(4000, '请输入承诺人');
                }
                if(empty($param['chentel'])){
                    return outputJson(4000, '请输入承诺人联系电话');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginShuiModel::insertGetId($param);
                (new OptService())->relation_file($files,'engin_shui',$id);
                (new FlowService())->save_bill('engin_shui', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_shui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_shui', $id, '税款承诺书');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['state'] = (new EnginService())->get_engin_shui_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('engin_shui');
            $data['flowsetname'] = FlowSetModel::get_name('engin_shui');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_shui');
            $data['isnext'] = (new FlowService())->check_isnext('engin_shui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_shui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入名称');
                }
                if(empty($param['chener'])){
                    return outputJson(4000, '请输入承诺人');
                }
                if(empty($param['chentel'])){
                    return outputJson(4000, '请输入承诺人联系电话');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签订状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择 签章类型');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginShuiModel::update($param);

                (new OptService())->relation_file($files,'engin_shui',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_shui', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_shui', $param['id']);
                }


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_shui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_shui', $param['id'], '税款承诺书');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginShuiModel::where(['id'=>$id])->find();
            if(!$data['data'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['state'] = (new EnginService())->get_engin_shui_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();

            $data['files'] = (new OptService())->get_file('engin_shui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_shui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_shui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_shui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_engin_cha(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['chatype'])){
                    return outputJson(4000, '请选择检查类型');
                }
                if(empty($param['chaer'])){
                    return outputJson(4000, '请输入检查人');
                }
                if(empty($param['chadt'])){
                    return outputJson(4000, '请选择检查日期');
                }
                if(empty($param['chagai'])){
                    return outputJson(4000, '请输入检查概况');
                }
                if(empty($param['isxu'])){
                    return outputJson(4000, '请选择是否需整改');
                }
                if(empty($param['iszheng'])){
                    return outputJson(4000, '请选择是否已整改');
                }
            }

            if($param['isxu'] != '是'){
                $param['gaiqiu'] = '';
                $param['gaier'] = '';
                $param['yaodt'] = '';
                $param['iszheng'] = '';
                $param['zhengdt'] = '';
                $param['gaishuo'] = '';
            }
            if($param['iszheng'] != '是'){
                $param['zhengdt'] = '';
                $param['gaishuo'] = '';
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginChaModel::insertGetId($param);

                (new FlowService())->save_bill('engin_cha', $id);
                (new OptService())->relation_file($files,'engin_cha',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_cha', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_cha', $id, '工程检查');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);


            $data['chatype'] = (new OptService())->get_base_info('engin_chatype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('engin_cha');
            $data['flowsetname'] = FlowSetModel::get_name('engin_cha');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_cha');
            $data['isnext'] = (new FlowService())->check_isnext('engin_cha');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_cha(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['chatype'])){
                    return outputJson(4000, '请选择检查类型');
                }
                if(empty($param['chaer'])){
                    return outputJson(4000, '请输入检查人');
                }
                if(empty($param['chadt'])){
                    return outputJson(4000, '请选择检查日期');
                }
                if(empty($param['chagai'])){
                    return outputJson(4000, '请输入检查概况');
                }
                if(empty($param['isxu'])){
                    return outputJson(4000, '请选择是否需整改');
                }
                if(empty($param['iszheng'])){
                    return outputJson(4000, '请选择是否已整改');
                }
            }

            if($param['isxu'] != '是'){
                $param['gaiqiu'] = '';
                $param['gaier'] = '';
                $param['yaodt'] = '';
                $param['iszheng'] = '';
                $param['zhengdt'] = '';
                $param['gaishuo'] = '';
            }
            if($param['iszheng'] != '是'){
                $param['zhengdt'] = '';
                $param['gaishuo'] = '';
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                EnginChaModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_cha', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_cha', $param['id']);
                }

                (new OptService())->relation_file($files,'engin_cha',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_cha', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_cha', $param['id'], '工程检查');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginChaModel::where(['id'=>$id])->find();
            if(!$data['data'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['chatype'] = (new OptService())->get_base_info('engin_chatype');

            $data['files'] = (new OptService())->get_file('engin_cha', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_cha');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_cha',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_cha');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function add_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入协议名称');
                }
                if(empty($param['gofuer'])){
                    return outputJson(4000, '请输入工程负责人');
                }
                if(empty($param['gofutel'])){
                    return outputJson(4000, '请输入工程负责人联系电话');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = ContractNeiModel::insertGetId($param);
                (new OptService())->relation_file($files,'contract_nei',$id);
                (new FlowService())->save_bill('contract_nei', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_nei', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_nei', $id, '工程管理文件汇编');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);


            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_nei');
            $data['flowsetname'] = FlowSetModel::get_name('contract_nei');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_nei');
            $data['isnext'] = (new FlowService())->check_isnext('contract_nei');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入协议名称');
                }
                if(empty($param['gofuer'])){
                    return outputJson(4000, '请输入工程负责人');
                }
                if(empty($param['gofutel'])){
                    return outputJson(4000, '请输入工程负责人联系电话');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                ContractNeiModel::update($param);

                (new OptService())->relation_file($files,'contract_nei',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_nei', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_nei', $param['id']);
                }


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_nei', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_nei', $param['id'], '工程管理文件汇编');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractNeiModel::where(['id'=>$id])->find();
            if(!$data['data'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();

            $data['files'] = (new OptService())->get_file('contract_nei', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_nei');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_nei',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_nei');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_contract_qu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签订状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = ContractQuModel::insertGetId($param);

                (new FlowService())->save_bill('contract_qu', $id);
                (new OptService())->relation_file($files,'contract_qu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_qu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_qu', $id, '分公司（办事处）合作经营');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['qiantype'] = (new OptService())->get_base_info('contract_nei_qiantype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_qu');
            $data['flowsetname'] = FlowSetModel::get_name('contract_qu');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_qu');
            $data['isnext'] = (new FlowService())->check_isnext('contract_qu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_qu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
                if($param['state'] === ''){
                    return outputJson(4000, '请选择签订状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                ContractQuModel::update($param);


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_qu', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_qu', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_qu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_qu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_qu', $param['id'], '分公司（办事处）合作经营');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractQuModel::where(['id'=>$id])->find();
            if(!$data['data'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['gui_unit_info'] = (new UserService())->get_users_part_arr($data['data']['gui_unit'], 'dept');
            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['qiantype'] = (new OptService())->get_base_info('contract_nei_qiantype');

            $data['files'] = (new OptService())->get_file('contract_qu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_qu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_qu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_qu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择主合同');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入补充合同名称');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同金额');
                }

                $shi_enginid = ContractShiModel::where(['id'=>$param['shiid']])->value('enginid');
                if($param['enginid'] != $shi_enginid){
                    return outputJson(4000, '工程名称与主合同关联项目不匹配，请重新选择');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                $id = ContractBuModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['buid'] = $id;
                        ContractShiGongModel::insert($item);
                    }
                }

                (new FlowService())->save_bill('contract_bu', $id);
//                (new EnginRelevan())->vate_engin_shimoney($param['enginid']);
                (new OptService())->relation_file($files,'contract_bu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bu', $id, '补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $shiid = $request->param('shiid', 0, 'int');
            $shiinfo = ContractShiModel::where(['id'=>$shiid])->find();
            if($shiinfo){
                $shiinfo['chenger'] = DeptModel::where(['id'=>$shiinfo['chenger']])->value('name');
                $data['shiinfo'] = $shiinfo;

                $engininfo = EnginModel::get_info_by_pk($shiinfo['enginid']);
                $data['engininfo'] = $engininfo;
            }


            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_bu');
            $data['flowsetname'] = FlowSetModel::get_name('contract_bu');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_bu');
            $data['isnext'] = (new FlowService())->check_isnext('contract_bu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractBuModel::where(['id'=>$param['id']])->find();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['shiid'])){
                    return outputJson(4000, '请选择主合同');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入补充合同名称');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同金额');
                }

                $shi_enginid = ContractShiModel::where(['id'=>$param['shiid']])->value('enginid');
                if($param['enginid'] != $shi_enginid){
                    return outputJson(4000, '工程名称与主合同关联项目不匹配，请重新选择');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                ContractBuModel::update($param);

                ContractShiGongModel::where(['buid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['buid'] = $param['id'];
                        ContractShiGongModel::insert($item);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_bu', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_bu', $param['id']);
                }

//                (new EnginRelevan())->vate_engin_shimoney($old['enginid']);
                (new OptService())->relation_file($files,'contract_bu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bu', $param['id'], '补充合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $shiinfo = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();
            if($shiinfo){
                $shiinfo['chenger'] = DeptModel::where(['id'=>$shiinfo['chenger']])->value('name');
                $data['shiinfo'] = $shiinfo;

                $engininfo = EnginModel::get_info_by_pk($shiinfo['enginid']);
                $data['engininfo'] = $engininfo;
            }

            $list = ContractShiGongModel::where(['buid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('contract_bu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_bu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_bu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_bu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择销售方(乙方)');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }

                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择采购类型');
                }
                if(empty($param['hetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择购买方(甲方)');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择合同状态');
                }
                if(empty($param['qiandt'])){
                    return outputJson(4000, '请选择签订日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }





            Db::startTrans();
            try {
                $id = ContractCaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    ContractCaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_cai', $id);

                (new OptService())->relation_file($files,'contract_cai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_cai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_cai', $id, '采购合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['caier_info'] = (new UserService())->get_users_part_arr(1, 'dept');
            $data['caitype'] = (new OptService())->get_base_info('engin_cai_caitype');
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['hetype'] = (new ContractService())->get_contract_hetype();
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_cai');
            $data['flowsetname'] = FlowSetModel::get_name('contract_cai');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_cai');
            $data['isnext'] = (new FlowService())->check_isnext('contract_cai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择销售方(乙方)');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }

                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择采购类型');
                }
                if(empty($param['hetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择购买方(甲方)');
                }
                if($param['state'] === ''){
                    return outputJson(4000, '请选择合同状态');
                }
                if(empty($param['qiandt'])){
                    return outputJson(4000, '请选择签订日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入合同明细');
                }
            }

            Db::startTrans();
            try {
                ContractCaiModel::update($param);

                ContractCaiListModel::where(['caiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    ContractCaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_cai', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_cai', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_cai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_cai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_cai', $param['id'], '采购合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['caier_info'] = (new UserService())->get_users_part_arr($data['data']['caier'], 'dept');


            $list = ContractCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;


            $data['caitype'] = (new OptService())->get_base_info('engin_cai_caitype');
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['hetype'] = (new ContractService())->get_contract_hetype();
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');

            $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_cai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_cai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_cai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_kaoshi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入纠纷名称');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择处理状态');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = KaoshiModel::insertGetId($param);


                (new FlowService())->save_bill('kaoshi', $id);

                (new OptService())->relation_file($files,'kaoshi',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kaoshi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kaoshi', $id, '工程纠纷');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['state'] = (new XingService())->get_kaoshi_state();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('kaoshi');
            $data['flowsetname'] = FlowSetModel::get_name('kaoshi');
            $data['isadvan'] = (new FlowService())->check_isadvan('kaoshi');
            $data['isnext'] = (new FlowService())->check_isnext('kaoshi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kaoshi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入纠纷名称');
                }
                if(empty($param['state'])){
                    return outputJson(4000, '请选择处理状态');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                KaoshiModel::update($param);



                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kaoshi', $param['id']);
                }else{
                    (new FlowService())->save_bill('kaoshi', $param['id']);
                }

                (new OptService())->relation_file($files,'kaoshi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kaoshi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kaoshi', $param['id'], '工程纠纷');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KaoshiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $data['state'] = (new XingService())->get_kaoshi_state();

            $data['files'] = (new OptService())->get_file('kaoshi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kaoshi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kaoshi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kaoshi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_kaobao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['isbao'])){
                    return outputJson(4000, '请选择是否投保');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);



            if($param['isbao'] != '是'){
                $param['xianlei'] = '';
                $list = [];
            }


            Db::startTrans();
            try {
                $id = KaoBaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['baoid'] = $id;
                    KaoBaoListModel::insert($vv);
                }


                (new FlowService())->save_bill('kaobao', $id);

                (new OptService())->relation_file($files,'kaobao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kaobao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kaobao', $id, '工程保险');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['zhifang'] = (new XingService())->get_kaobao_zhifang();
            $data['xianlei'] = (new OptService())->get_base_info('kaobao_xianlei');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('kaobao');
            $data['flowsetname'] = FlowSetModel::get_name('kaobao');
            $data['isadvan'] = (new FlowService())->check_isadvan('kaobao');
            $data['isnext'] = (new FlowService())->check_isnext('kaobao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kaobao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['isbao'])){
                    return outputJson(4000, '请选择是否投保');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isbao'] != '是'){
                $param['xianlei'] = '';
                $list = [];
            }


            Db::startTrans();
            try {
                KaoBaoModel::update($param);

                KaoBaoListModel::where(['baoid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['baoid'] = $param['id'];
                    KaoBaoListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kaobao', $param['id']);
                }else{
                    (new FlowService())->save_bill('kaobao', $param['id']);
                }

                (new OptService())->relation_file($files,'kaobao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kaobao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kaobao', $param['id'], '工程保险');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KaoBaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['xianlei'] = explode(',',$data['data']['xianlei']);

            $data['zhifang'] = (new XingService())->get_kaobao_zhifang();
            $data['xianlei'] = (new OptService())->get_base_info('kaobao_xianlei');

            $list = KaoBaoListModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('kaobao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kaobao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kaobao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kaobao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_cai_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择采购合同');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入明细信息');
                }
            }


            Db::startTrans();
            try {
                $id = ContractCaiJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    ContractCaiJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_cai_jie', $id);

                (new OptService())->relation_file($files,'contract_cai_jie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_cai_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_cai_jie', $id, '采购合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_cai_jie');
            $data['flowsetname'] = FlowSetModel::get_name('contract_cai_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_cai_jie');
            $data['isnext'] = (new FlowService())->check_isnext('contract_cai_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_cai_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择采购合同');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入明细信息');
                }
            }


            Db::startTrans();
            try {
                ContractCaiJieModel::update($param);

                ContractCaiJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    ContractCaiJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_cai_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_cai_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_cai_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_cai_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_cai_jie', $param['id'], '采购合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $caiinfo = ContractCaiModel::get_info_by_pk($data['data']['caiid']);
            $data['caiinfo'] = $caiinfo;


            $list = ContractCaiJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('contract_cai_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_cai_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_cai_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_cai_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_contract_chu_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['chuid'])){
                    return outputJson(4000, '请选择储备合同');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = ContractChuJieModel::insertGetId($param);


                (new FlowService())->save_bill('contract_chu_jie', $id);

                (new OptService())->relation_file($files,'contract_chu_jie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_chu_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_chu_jie', $id, '储备合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_chu_jie');
            $data['flowsetname'] = FlowSetModel::get_name('contract_chu_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_chu_jie');
            $data['isnext'] = (new FlowService())->check_isnext('contract_chu_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_chu_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['chuid'])){
                    return outputJson(4000, '请选择储备合同');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ContractChuJieModel::update($param);



                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_chu_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_chu_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_chu_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_chu_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_chu_jie', $param['id'], '储备合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractChuJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $chuinfo = ContractChuModel::get_info_by_pk($data['data']['chuid']);
//            if($chuinfo){
//                $chuinfo['leiyifu'] = PaymentEnginListModel::alias('list')
//                    ->join('payment_engin en','en.id=list.payenid')
//                    ->where(['en.deleted_user'=>0,'en.isturn'=>1,'en.status'=>1,'list.futype'=>1,'list.caiid'=>$chuinfo['id']])->sum('list.money');
//
//                $chuinfo['heleijinfee'] = PiaoChuModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'chuid'=>$chuinfo['id']])->sum('money');
//                $chuinfo['heleijiefee'] = ContractChuJieModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'chuid'=>$chuinfo['id']])->sum('money');
//
//            }
            $data['chuinfo'] = $chuinfo;



            $data['files'] = (new OptService())->get_file('contract_chu_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_chu_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_chu_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_chu_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_contract_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['number'])){
                    return outputJson(4000, '请输入合同编号');
                }
                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择采购类型');
                }
                if(empty($param['hetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择购买方(甲方)');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签订状态');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进项票明细');
                }
            }

            Db::startTrans();
            try {
                $id = ContractChuModel::insertGetId($param);


                foreach($list as $vv){
                    $vv['chuid'] = $id;
                    $vv['type'] = 1;
                    ContractChuListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_chu', $id);

                (new OptService())->relation_file($files,'contract_chu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_chu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_chu', $id, '储备合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['caitype'] = (new OptService())->get_base_info('engin_cai_caitype');
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['hetype'] = (new ContractService())->get_contract_hetype();
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_chu');
            $data['flowsetname'] = FlowSetModel::get_name('contract_chu');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_chu');
            $data['isnext'] = (new FlowService())->check_isnext('contract_chu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择供应方(乙方)');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['number'])){
                    return outputJson(4000, '请输入合同编号');
                }
                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择采购类型');
                }
                if(empty($param['hetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['caier'])){
                    return outputJson(4000, '请选择购买方(甲方)');
                }
                if($param['state'] === ''){
                    return outputJson(4000, '请选择签订状态');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进项票明细');
                }
            }

            Db::startTrans();
            try {
                ContractChuModel::update($param);

                ContractChuListModel::where(['chuid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['chuid'] = $param['id'];
                    $vv['type'] = 1;
                    ContractChuListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_chu', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_chu', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_chu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_chu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_chu', $param['id'], '储备合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractChuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['liantel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'lianer'=>$data['data']['lianer']])->value('liantel');

            $data['data']['caier_info'] = (new UserService())->get_users_part_arr($data['data']['caier'], 'dept');

            $data['caitype'] = (new OptService())->get_base_info('engin_cai_caitype');
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['hetype'] = (new ContractService())->get_contract_hetype();
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');


            $list = ContractChuListModel::where(['chuid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $listinfo = PiaoChuListModel::where(['id'=>$item['piaolistid']])->find();

                $item['nums'] = $listinfo['nums'];
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $listinfo['qianpiaotype']);
                $item['kaidt'] = $listinfo['kaidt'];
                $item['cailei'] = $listinfo['cailei'];
                $item['fee'] = $listinfo['fee'];
                $item['rate'] = $listinfo['fee'];
                $item['listratefee'] = $listinfo['ratefee'];
                $item['listmoney'] = $listinfo['money'];

                $item['yifufee'] = HouChujinGuanModel::alias('guan')
                    ->field('list.*')
                    ->join('hou_chujin jin','jin.id=guan.chujinid')
                    ->join('piao_chu piao','piao.id=guan.piaoid')
                    ->where(['jin.comid'=>$this->userinfo['comid'],'jin.deleted_user'=>0,'jin.isturn'=>1,'jin.status'=>1])
                    ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1,'piao.status'=>1])
                    ->where(['guan.piaolistid'=>$item['piaolistid']])
                    ->sum('difee');
                if($item['yifufee'] <= 0){
                    $item['statename'] = '未付款';
                }else{
                    if($item['yifufee'] < $item['listmoney']){
                        $item['statename'] = '部分付款';
                    }else{
                        $item['statename'] = '已付款';
                    }
                }
                $item['daifufee'] = round($item['listmoney'] - $item['yifufee'], 2);
            }
            $data['list'] = $list;



            $data['files'] = (new OptService())->get_file('contract_chu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_chu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_chu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_chu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_xing(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择合作方');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            if($param['isturn'] > 0){
                if(count($files) < 1){
                    return outputJson(4000, '请至少上传一个文件');
                }
            }


            Db::startTrans();
            try {
                $id = ContractXingModel::insertGetId($param);


                (new FlowService())->save_bill('contract_xing', $id);

                (new OptService())->relation_file($files,'contract_xing',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_xing', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_xing', $id, '行政合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['caitype'] = (new OptService())->get_base_info('engin_xing_caitype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_xing');
            $data['flowsetname'] = FlowSetModel::get_name('contract_xing');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_xing');
            $data['isnext'] = (new FlowService())->check_isnext('contract_xing');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_xing(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['caitype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['gonger'])){
                    return outputJson(4000, '请选择合作方');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            if($param['isturn'] > 0){
                if(count($files) < 1){
                    return outputJson(4000, '请至少上传一个文件');
                }
            }

            Db::startTrans();
            try {
                ContractXingModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_xing', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_xing', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_xing',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_xing', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_xing', $param['id'], '行政合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractXingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['caitype'] = (new OptService())->get_base_info('engin_xing_caitype');
            $data['data']['gui_unit_info'] = (new UserService())->get_users_part_arr($data['data']['gui_unit'], 'dept');


            $data['files'] = (new OptService())->get_file('contract_xing', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_xing');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_xing',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_xing');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_shi_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            Db::startTrans();
            try {
                $id = ContractShiJieModel::insertGetId($param);


                (new FlowService())->save_bill('contract_shi_jie', $id);

                (new OptService())->relation_file($files,'contract_shi_jie',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_shi_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_shi_jie', $id, '施工合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_shi_jie');
            $data['flowsetname'] = FlowSetModel::get_name('contract_shi_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_shi_jie');
            $data['isnext'] = (new FlowService())->check_isnext('contract_shi_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_shi_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入结算主题');
                }
                if(empty($param['jiedt'])){
                    return outputJson(4000, '请选择结算日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                ContractShiJieModel::update($param);



                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_shi_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_shi_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_shi_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_shi_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_shi_jie', $param['id'], '施工合同结算');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $data['files'] = (new OptService())->get_file('contract_shi_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_shi_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_shi_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_shi_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择变更日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经办人');
                }

                $cai_enginid = ContractCaiModel::where(['id'=>$param['caiid']])->value('enginid');
                if($param['enginid'] != $cai_enginid){
                    return outputJson(4000, '工程名称与合同名称关联项目不匹配，请重新选择');
                }


                $exist = ContractBianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'caiid'=>$param['caiid']])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }
            }


            $cai_money = ContractCaiModel::where(['id'=>$param['caiid']])->value('money');

            $param['yuanfee'] = $cai_money;


            $param['number'] = (new ContractService())->create_contract_bian_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入变更明细');
                }
            }



            Db::startTrans();
            try {
                $id = ContractBianModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['bianid'] = $id;
                    ContractBianListModel::insert($vv);
                }

                (new FlowService())->save_bill('contract_bian', $id);
                (new OptService())->relation_file($files,'contract_bian',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bian', $id, '合同变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $caiid = $request->param('caiid', 0, 'int');


            $caiinfo = ContractCaiModel::where(['id'=>$caiid])->find();
            if($caiinfo){
                $data['caiinfo'] = $caiinfo;

                $engininfo = EnginModel::get_info_by_pk($caiinfo['enginid']);
                if($engininfo){
                    $engininfo['budgetid'] = EnginBudgetModel::where(['enginid'=>$engininfo['id'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');
                }
                $data['engininfo'] = $engininfo;
            }



            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new ContractService())->create_contract_bian_number();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_bian');
            $data['flowsetname'] = FlowSetModel::get_name('contract_bian');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_bian');
            $data['isnext'] = (new FlowService())->check_isnext('contract_bian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractBianModel::where(['id'=>$param['id']])->find();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['caiid'])){
                    return outputJson(4000, '请选择合同名称');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择变更日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经办人');
                }

                $cai_enginid = ContractCaiModel::where(['id'=>$param['caiid']])->value('enginid');
                if($param['enginid'] != $cai_enginid){
                    return outputJson(4000, '工程名称与合同名称关联项目不匹配，请重新选择');
                }

                $exist = ContractBianModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>0,'caiid'=>$param['caiid']])
                    ->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '存在审核中的变更申请，不能重复提交');
                }
            }


            $cai_money = ContractCaiModel::where(['id'=>$param['caiid']])->value('money');

            $param['yuanfee'] = $cai_money;


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入变更明细');
                }
            }


            Db::startTrans();
            try {
                ContractBianModel::update($param);


                ContractBianListModel::where(['bianid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['bianid'] = $param['id'];
                    ContractBianListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_bian', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_bian', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_bian',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bian', $param['id'], '合同变更');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $caiinfo = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
            $data['caiinfo'] = $caiinfo;
            $engininfo = EnginModel::get_info_by_pk($caiinfo['enginid']);
            $data['engininfo'] = $engininfo;
            $data['data']['budgetid'] = EnginBudgetModel::where(['enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');

            $list = ContractBianListModel::where(['bianid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['subname'] = '';
                $item['guiname'] = '';
                $listinfo = EnginBudgetListModel::where(['id'=>$item['subid']])->find();
                if($listinfo){
                    $item['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
                    $item['guiname'] = $listinfo['name'];
                }
            }
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('contract_bian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_bian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_bian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_bian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }

                $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
                }

                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['number'])){
                    return outputJson(4000, '请输入合同编号');
                }
                if(empty($param['shihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['faer'])){
                    return outputJson(4000, '请选择(发包人)甲方');
                }
                if(empty($param['chenger'])){
                    return outputJson(4000, '请选择承包人(乙方)');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同价');
                }
                if(empty($param['qiandt'])){
                    return outputJson(4000, '请选择签订日期');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            unset($param['record']);
            unset($param['neiid']);

            $list = array_filter($param['list']);
            unset($param['list']);



            Db::startTrans();
            try {
                $id = ContractShiModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shiid'] = $id;
                        ContractShiGongModel::insert($item);
                    }
                }

                (new FlowService())->save_bill('contract_shi', $id);

                (new OptService())->relation_file($files,'contract_shi',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_shi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_shi', $id, '施工合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['zhilevel'] = (new ContractService())->get_contract_shi_zhilevel();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('contract_shi');
            $data['flowsetname'] = FlowSetModel::get_name('contract_shi');
            $data['isadvan'] = (new FlowService())->check_isadvan('contract_shi');
            $data['isnext'] = (new FlowService())->check_isnext('contract_shi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }

                $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                    ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
                if($exist){
                    return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
                }


                if(empty($param['name'])){
                    return outputJson(4000, '请输入合同名称');
                }
                if(empty($param['number'])){
                    return outputJson(4000, '请输入合同编号');
                }
                if(empty($param['shihetype'])){
                    return outputJson(4000, '请选择合同类型');
                }
                if(empty($param['faer'])){
                    return outputJson(4000, '请选择(发包人)甲方');
                }
                if(empty($param['chenger'])){
                    return outputJson(4000, '请选择承包人(乙方)');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入合同价');
                }
                if(empty($param['qiandt'])){
                    return outputJson(4000, '请选择签订日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                ContractShiModel::update($param);

                ContractShiGongModel::where(['shiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shiid'] = $param['id'];
                        ContractShiGongModel::insert($item);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('contract_shi', $param['id']);
                }else{
                    (new FlowService())->save_bill('contract_shi', $param['id']);
                }

                (new OptService())->relation_file($files,'contract_shi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_shi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_shi', $param['id'], '施工合同');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['data']['chenger_info'] = (new UserService())->get_users_part_arr($data['data']['chenger'], 'dept');



            $list = ContractShiGongModel::where(['shiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['zhilevel'] = (new ContractService())->get_contract_shi_zhilevel();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('contract_shi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('contract_shi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('contract_shi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入工程名称');
                }
                if((new EnginService())->check_engin_name($param['name'])){
                    return outputJson(4000, '工程名称已存在');
                }
                if(empty($param['jiaer'])){
                    return outputJson(4000, '请输入甲方名称');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
                if(empty($param['lanshi'])){
                    return outputJson(4000, '请选择承揽方式');
                }

                if($param['lantake'] == 2){

                    if(empty($param['kailei'])){
                        return outputJson(4000, '请选择工程性质');
                    }
                    if(empty($param['zhaotype'])){
                        return outputJson(4000, '请选择招标方式');
                    }
                    if(empty($param['win_unit'])){
                        return outputJson(4000, '请选择中标单位');
                    }
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择工程状态');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = EnginModel::insertGetId($param);

                (new FlowService())->save_bill('engin', $id);

                (new OptService())->relation_file($files,'engin',$id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin', $id, '工程承揽');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $bidid = $request->get('bidid',0,'int');
            $data['data'] = BidModel::where(['id'=>$bidid])->find();
            if($data['data']){
                $data['data']['gofutel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                    ->where(['name'=>$data['data']['gofuer']])->value('liantel');

                $data['data']['win_unit_info'] = (new UserService())->get_users_part_arr($data['data']['tou_unit'], 'dept');
                $data['data']['gui_unit_info'] = (new UserService())->get_users_part_arr($data['data']['gui_unit'], 'dept');
            }


            $data['lantake'] = (new EnginService())->get_engin_lantake();
            $data['state'] = (new EnginService())->get_engin_state();
            $data['areas'] = (new OptService())->get_city_cader();
            $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
            $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
            $data['gonghelei'] = (new OptService())->get_base_info('engin_gonghelei');
            $data['areas'] = (new OptService())->get_city_cader();


            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('engin');
            $data['flowsetname'] = FlowSetModel::get_name('engin');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin');
            $data['isnext'] = (new FlowService())->check_isnext('engin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入工程名称');
                }
                if((new EnginService())->check_engin_name($param['name'], $param['id'])){
                    return outputJson(4000, '工程名称已存在');
                }
                if(empty($param['jiaer'])){
                    return outputJson(4000, '请输入甲方名称');
                }
                if(empty($param['gui_unit'])){
                    return outputJson(4000, '请选择业务归属');
                }
                if(empty($param['lanshi'])){
                    return outputJson(4000, '请选择承揽方式');
                }
                if($param['lantake'] == 2){

                    if(empty($param['kailei'])){
                        return outputJson(4000, '请选择工程性质');
                    }
                    if(empty($param['zhaotype'])){
                        return outputJson(4000, '请选择招标方式');
                    }
                    if(empty($param['win_unit'])){
                        return outputJson(4000, '请选择中标单位');
                    }
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择工程状态');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                EnginModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin', $param['id']);

                }else{
                    (new FlowService())->save_bill('engin', $param['id']);
                }

                (new OptService())->relation_file($files,'engin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin', $param['id'], '工程承揽');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['win_unit_info'] = (new UserService())->get_users_part_arr($data['data']['win_unit'], 'dept');
            $data['data']['gui_unit_info'] = (new UserService())->get_users_part_arr($data['data']['gui_unit'], 'dept');

            $data['lantake'] = (new EnginService())->get_engin_lantake();
            $data['state'] = (new EnginService())->get_engin_state();
            $data['lanshi'] = (new OptService())->get_base_info('bid_lanshi');
            $data['kailei'] = (new OptService())->get_base_info('bid_kailei');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_zhaotype');
            $data['gonghelei'] = (new OptService())->get_base_info('engin_gonghelei');
            $data['areas'] = (new OptService())->get_city_cader();


            $data['files'] = (new OptService())->get_file('engin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zitype'])){
                    return outputJson(4000, '请选择资料类型');
                }

            }


            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();
            $newparam['flowuser'] = $this->userinfo['realname'];
            $newparam['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }


            $newparam['enginid'] = $param['enginid'];
            $newparam['isturn'] = $param['isturn'];
            $newparam['nexter'] = $param['nexter'];
            $newparam['enginid'] = $param['enginid'];
            $newparam['zitype'] = $param['zitype'];
            $newparam['zitype'] = $param['zitype'];
            $newparam['flowrelas'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';

            Db::startTrans();
            try {
                $id = EnginStampModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['stampid'] = $id;
                    EnginStampListModel::insert($vv);
                }

                (new FlowService())->save_bill('engin_stamp', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'engin_stamp', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'engin_stamp', $id, '项目资料盖章');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{

            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            $data['flowuser'] = $this->userinfo['realname'];
//            $data['files'] = (new OptService())->get_file('engin_stamp');
            $data['flowsetname'] = FlowSetModel::get_name('engin_stamp');
            $data['isadvan'] = (new FlowService())->check_isadvan('engin_stamp');
            $data['isnext'] = (new FlowService())->check_isnext('engin_stamp');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_engin_stamp(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zitype'])){
                    return outputJson(4000, '请选择资料类型');
                }

            }


            $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }

            }


            $newparam['id'] = $param['id'];
            $newparam['isturn'] = $param['isturn'];
            $newparam['nexter'] = $param['nexter'];
            $newparam['enginid'] = $param['enginid'];
            $newparam['zitype'] = $param['zitype'];
            $newparam['flowrelas'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';

            Db::startTrans();
            try {
                EnginStampModel::update($newparam);

                EnginStampListModel::where(['stampid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['stampid'] = $param['id'];
                    EnginStampListModel::insert($vv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('engin_stamp', $param['id']);
                }else{
                    (new FlowService())->save_bill('engin_stamp', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'engin_stamp', $param['id'], '项目资料盖章');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = EnginStampModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;


            $list = EnginStampListModel::where(['stampid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['yongtypemob'] = $item['yongtype'];
                $item['yongtype'] = explode(',', $item['yongtype']);
                $item['yongtype_name'] = (new OptService())->get_base_showname_duo('scene_yongtype', $item['yongtype']);
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;

            $data['zitype'] = (new OptService())->get_base_info('scene_zitype');
            $data['yongtype'] = (new OptService())->get_base_info('scene_yongtype');
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('engin_stamp');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('engin_stamp',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('engin_stamp');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    //##############################################  其他  ##############################################
    //##############################################  其他  ##############################################


    public function add_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();



            $newparam['comid'] = $this->userinfo['comid'];
            $newparam['created_user'] = $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['created_at'] = $newparam['updated_at']= date('Y-m-d H:i:s');
            $newparam['deptid'] = $this->userinfo['deptid'];
            $newparam['chanid'] = (new UserService())->get_user_chanid();
            $newparam['flowuser'] = $this->userinfo['realname'];
            $newparam['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }


            $newparam['isturn'] = $param['isturn'];
            $newparam['nexter'] = $param['nexter'];
            $newparam['advaners'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';
            $newparam['con'] = $param['con'];


            Db::startTrans();
            try {
                $id = SealGaiModel::insertGetId($newparam);

                foreach($list as $vv){
                    $vv['gaiid'] = $id;
                    SealGaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('seal_gai', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_gai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_gai', $id, '日常用印');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['yongtype'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();


            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('seal_gai');
            $data['flowsetname'] = FlowSetModel::get_name('seal_gai');
            $data['isadvan'] = (new FlowService())->check_isadvan('seal_gai');
            $data['isnext'] = (new FlowService())->check_isnext('seal_gai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_gai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $newparam['updated_user'] = $this->userinfo['id'];
            $newparam['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $newparam['created_at'] = date('Y-m-d H:i:s');
            }

            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入盖章资料明细');
                }
            }


            $newparam['id'] = $param['id'];
            $newparam['isturn'] = $param['isturn'];
            $newparam['nexter'] = $param['nexter'];
            $newparam['flowrelas'] = isset($param['flowrelas']) ? $param['flowrelas'] : '';
            $newparam['advaners'] = isset($param['advaners']) ? $param['advaners'] : '';
            $newparam['con'] = $param['con'];
            Db::startTrans();
            try {
                SealGaiModel::update($newparam);

                SealGaiListModel::where(['gaiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gaiid'] = $param['id'];
                    SealGaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('seal_gai', $param['id']);
                }else{
                    (new FlowService())->save_bill('seal_gai', $param['id']);
                }
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'seal_gai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'seal_gai', $param['id'], '日常用印');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealGaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['yongtype'] = SealModel::field('name as title,name as value')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->select()->toArray();

            $list = SealGaiListModel::where(['gaiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['filearr'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['files'] = FileModel::where('id','in',$item['files'])->select()->toArray();
                $item['filesnum'] = count($item['files']);
            }
            $data['list'] = $list;
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('seal_gai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('seal_gai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('seal_gai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_book_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入借阅图书明细');
                }
            }


            Db::startTrans();
            try {
                $id = BookJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    $vv['yudt'] = $param['yudt'];
                    BookJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('book_jie', $id);

                (new OptService())->relation_file($files,'book_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'book_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'book_jie', $id, '图书借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['jieer_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('book_jie');
            $data['flowsetname'] = FlowSetModel::get_name('book_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('book_jie');
            $data['isnext'] = (new FlowService())->check_isnext('book_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_book_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入借阅图书明细');
                }
            }

            Db::startTrans();
            try {
                BookJieModel::update($param);

                BookJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    $vv['yudt'] = $param['yudt'];
                    BookJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('book_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('book_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'book_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'book_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'book_jie', $param['id'], '图书借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BookJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['jieer_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'user');


            $list = BookJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $bookinfo = BookModel::where(['id'=>$item['bookid']])->find();
                if($bookinfo){
                    $item['name'] = $bookinfo['name'];
                    $item['number'] = $bookinfo['number'];
                    $item['cunaddr'] = $bookinfo['cunaddr'];
                }
            }
            $data['list'] = $list;




            $data['files'] = (new OptService())->get_file('book_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('book_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('book_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('book_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_port(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['huilei'])){
                    return outputJson(4000, '请选择汇报类型');
                }
                if(empty($param['zhoustartdt'])){
                    return outputJson(4000, '请选择复盘周期');
                }
                if(empty($param['zhouenddt'])){
                    return outputJson(4000, '请选择复盘周期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            unset($param['wanqing']);

            $list_hui = array_filter($param['list_hui']);
            unset($param['list_hui']);

            $list_an = array_filter($param['list_an']);
            unset($param['list_an']);

            Db::startTrans();
            try {
                $id = PortModel::insertGetId($param);

                foreach($list_hui as $vv){
                    $vv['portid'] = $id;
                    PortHuiModel::insert($vv);
                }

                foreach($list_an as $vvv){
                    $vvv['portid'] = $id;
                    PortAnModel::insert($vvv);
                }

                (new FlowService())->save_bill('port', $id);

                (new OptService())->relation_file($files,'port',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'port', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'port', $id, '工作复盘及安排');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['huilei'] = (new OptService())->get_base_info('port_huilei');
            $data['anlei'] = (new OptService())->get_base_info('port_anlei');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('port');
            $data['flowsetname'] = FlowSetModel::get_name('port');
            $data['isadvan'] = (new FlowService())->check_isadvan('port');
            $data['isnext'] = (new FlowService())->check_isnext('port');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_port(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['huilei'])){
                    return outputJson(4000, '请选择汇报类型');
                }
                if(empty($param['zhoustartdt'])){
                    return outputJson(4000, '请选择复盘周期');
                }
                if(empty($param['zhouenddt'])){
                    return outputJson(4000, '请选择复盘周期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);
            unset($param['wanqing']);

            $list_hui = array_filter($param['list_hui']);
            unset($param['list_hui']);

            $list_an = array_filter($param['list_an']);
            unset($param['list_an']);


            Db::startTrans();
            try {
                PortModel::update($param);

                PortHuiModel::where(['portid'=>$param['id']])->delete();
                foreach($list_hui as $vv){
                    $vv['portid'] = $param['id'];
                    PortHuiModel::insert($vv);
                }

                PortAnModel::where(['portid'=>$param['id']])->delete();
                foreach($list_an as $vvv){
                    $vvv['portid'] = $param['id'];
                    PortAnModel::insert($vvv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('port', $param['id']);
                }else{
                    (new FlowService())->save_bill('port', $param['id']);
                }

                (new OptService())->relation_file($files,'port',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'port', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'port', $param['id'], '工作复盘及安排');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PortModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['huilei'] = (new OptService())->get_base_info('port_huilei');
            $data['anlei'] = (new OptService())->get_base_info('port_anlei');
            $list_hui = PortHuiModel::where(['portid'=>$data['data']['id']])->select()->toArray();
            $data['list_hui'] = $list_hui;

            $list_an = PortAnModel::where(['portid'=>$data['data']['id']])->select()->toArray();
            $data['list_an'] = $list_an;


            $data['files'] = (new OptService())->get_file('port', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('port');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('port',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('port');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_peng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入姓名');
                }
                if(empty($param['gouxing'])){
                    return outputJson(4000, '请选择沟通性质');
                }
                if(empty($param['goudt'])){
                    return outputJson(4000, '请选择沟通日期');
                }
                if(empty($param['qizhi'])){
                    return outputJson(4000, '请输入起止时间');
                }
                if(empty($param['gouci'])){
                    return outputJson(4000, '请输入本月沟通次数');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = PengModel::insertGetId($param);


                (new FlowService())->save_bill('peng', $id);

                (new OptService())->relation_file($files,'peng',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'peng', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'peng', $id, '工作沟通登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['name'] = $this->userinfo['realname'];
            $data['gouxing'] = (new OptService())->get_base_info('peng_gouxing');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('peng');
            $data['flowsetname'] = FlowSetModel::get_name('peng');
            $data['isadvan'] = (new FlowService())->check_isadvan('peng');
            $data['isnext'] = (new FlowService())->check_isnext('peng');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_peng(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入姓名');
                }
                if(empty($param['gouxing'])){
                    return outputJson(4000, '请选择沟通性质');
                }
                if(empty($param['goudt'])){
                    return outputJson(4000, '请选择沟通日期');
                }
                if(empty($param['qizhi'])){
                    return outputJson(4000, '请输入起止时间');
                }
                if(empty($param['gouci'])){
                    return outputJson(4000, '请输入本月沟通次数');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                PengModel::update($param);



                if($isshen > 0){
                    (new FlowService())->save_bill_shen('peng', $param['id']);
                }else{
                    (new FlowService())->save_bill('peng', $param['id']);
                }

                (new OptService())->relation_file($files,'peng',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'peng', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'peng', $param['id'], '工作沟通登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = PengModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['gouxing'] = (new OptService())->get_base_info('peng_gouxing');
            $data['pengids_info'] = PengModel::field('id,billid')->where('id','in',$data['data']['pengids'])->select()->toArray();
            foreach ($data['pengids_info'] as $k=>$v){
                $data['pengids_info'][$k]['sericnum'] = FlowBillModel::where(['id'=>$v['billid']])->value('sericnum');
            }

            $data['files'] = (new OptService())->get_file('peng', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('peng');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('peng',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('peng');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_doss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入档案借阅明细');
                }
            }


            Db::startTrans();
            try {
                $id = DossJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    DossJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('doss_jie', $id);

                (new OptService())->relation_file($files,'doss_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'doss_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'doss_jie', $id, '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['jieer_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('doss_jie');
            $data['flowsetname'] = FlowSetModel::get_name('doss_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('doss_jie');
            $data['isnext'] = (new FlowService())->check_isnext('doss_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_doss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入档案借阅明细');
                }
            }

            Db::startTrans();
            try {
                DossJieModel::update($param);

                DossJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    DossJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('doss_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('doss_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'doss_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'doss_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'doss_jie', $param['id'], '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DossJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['jieer_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'user');


            $list = DossJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $dossinfo = DossModel::where(['id'=>$item['dossid']])->find();
                if($dossinfo){
                    $item['dangfen_name'] = (new OptService())->get_base_showname('doss_dangfen', $dossinfo['dangfen']);

                    $item['name'] = $dossinfo['name'];
                    $item['number'] = $dossinfo['number'];
                    $item['enginname'] = EnginModel::where(['id'=>$dossinfo['enginid']])->value('name');


                    $item['yijie'] = DossJieListModel::alias('list')
                        ->join('doss_jie jie','jie.id=list.jieid')
                        ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1])
                        ->where(['list.dossid'=>$item['id'],'list.state'=>0])
                        ->sum('list.num');

                    $item['shenjie'] = DossJieListModel::alias('list')
                        ->join('doss_jie jie','jie.id=list.jieid')
                        ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                        ->where(['list.dossid'=>$item['id']])
                        ->where('jie.id','neq',$data['data']['id'])
                        ->sum('list.num');
                    $item['kejie'] = round($dossinfo['guinum'] - $item['yijie'] - $item['shenjie'], 2);
                }
            }
            $data['list'] = $list;


            $data['files'] = (new OptService())->get_file('doss_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('doss_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('doss_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('doss_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_coss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入档案借阅明细');
                }
            }


            Db::startTrans();
            try {
                $id = CossJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    CossJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('coss_jie', $id);

                (new OptService())->relation_file($files,'coss_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'coss_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'coss_jie', $id, '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['jieer_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('coss_jie');
            $data['flowsetname'] = FlowSetModel::get_name('coss_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('coss_jie');
            $data['isnext'] = (new FlowService())->check_isnext('coss_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_coss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入档案借阅明细');
                }
            }

            Db::startTrans();
            try {
                CossJieModel::update($param);

                CossJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    CossJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('coss_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('coss_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'coss_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'coss_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'coss_jie', $param['id'], '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CossJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['jieer_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'user');


            $list = CossJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $cossinfo = CossModel::where(['id'=>$item['cossid']])->find();
                if($cossinfo){
                    $item['dangfen_name'] = (new OptService())->get_base_showname('coss_dangfen', $cossinfo['dangfen']);

                    $item['name'] = $cossinfo['name'];
                    $item['number'] = $cossinfo['number'];
                    $item['enginname'] = EnginModel::where(['id'=>$cossinfo['enginid']])->value('name');


                    $item['yijie'] = CossJieListModel::alias('list')
                        ->join('coss_jie jie','jie.id=list.jieid')
                        ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1])
                        ->where(['list.cossid'=>$item['id'],'list.state'=>0])
                        ->sum('list.num');

                    $item['shenjie'] = CossJieListModel::alias('list')
                        ->join('coss_jie jie','jie.id=list.jieid')
                        ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                        ->where(['list.cossid'=>$item['id']])
                        ->where('jie.id','neq',$data['data']['id'])
                        ->sum('list.num');
                    $item['kejie'] = round($cossinfo['guinum'] - $item['yijie'] - $item['shenjie'], 2);
                }
            }
            $data['list'] = $list;


            $data['files'] = (new OptService())->get_file('coss_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('coss_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('coss_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('coss_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_xoss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入档案借阅明细');
                }
            }


            Db::startTrans();
            try {
                $id = XossJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    XossJieListModel::insert($vv);
                }

                (new FlowService())->save_bill('xoss_jie', $id);

                (new OptService())->relation_file($files,'xoss_jie',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'xoss_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'xoss_jie', $id, '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['jieer_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('xoss_jie');
            $data['flowsetname'] = FlowSetModel::get_name('xoss_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('xoss_jie');
            $data['isnext'] = (new FlowService())->check_isnext('xoss_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_xoss_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借阅人');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入档案借阅明细');
                }
            }

            Db::startTrans();
            try {
                XossJieModel::update($param);

                XossJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    XossJieListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('xoss_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('xoss_jie', $param['id']);
                }

                (new OptService())->relation_file($files,'xoss_jie',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'xoss_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'xoss_jie', $param['id'], '档案借阅');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = XossJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['jieer_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'user');


            $list = XossJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $xossinfo = XossModel::where(['id'=>$item['xossid']])->find();
                if($xossinfo){
                    $item['dangfen_name'] = (new OptService())->get_base_showname('xoss_dangfen', $xossinfo['dangfen']);

                    $item['name'] = $xossinfo['name'];
                    $item['number'] = $xossinfo['number'];
                    $item['enginname'] = EnginModel::where(['id'=>$xossinfo['enginid']])->value('name');


                    $item['yijie'] = XossJieListModel::alias('list')
                        ->join('xoss_jie jie','jie.id=list.jieid')
                        ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>1])
                        ->where(['list.xossid'=>$item['id'],'list.state'=>0])
                        ->sum('list.num');

                    $item['shenjie'] = XossJieListModel::alias('list')
                        ->join('xoss_jie jie','jie.id=list.jieid')
                        ->where(['jie.comid'=>$this->userinfo['comid'],'jie.deleted_user'=>0,'jie.isturn'=>1,'jie.status'=>0])
                        ->where(['list.xossid'=>$item['id']])
                        ->where('jie.id','neq',$data['data']['id'])
                        ->sum('list.num');
                    $item['kejie'] = round($xossinfo['guinum'] - $item['yijie'] - $item['shenjie'], 2);
                }
            }
            $data['list'] = $list;


            $data['files'] = (new OptService())->get_file('xoss_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('xoss_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('xoss_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('xoss_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_wagyuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }


            Db::startTrans();
            try {
                $id = WagyuanModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['wagid'] = $id;
                    WagyuanListModel::insert($vv);
                }

                (new FlowService())->save_bill('wagyuan', $id);

                (new OptService())->relation_file($files,'wagyuan',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wagyuan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wagyuan', $id, '员工工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('wagyuan');
            $data['flowsetname'] = FlowSetModel::get_name('wagyuan');
            $data['isadvan'] = (new FlowService())->check_isadvan('wagyuan');
            $data['isnext'] = (new FlowService())->check_isnext('wagyuan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wagyuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }


            Db::startTrans();
            try {
                WagyuanModel::update($param);

                WagyuanListModel::where(['wagid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['wagid'] = $param['id'];
                    WagyuanListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wagyuan', $param['id']);
                }else{
                    (new FlowService())->save_bill('wagyuan', $param['id']);
                }

                (new OptService())->relation_file($files,'wagyuan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wagyuan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wagyuan', $param['id'], '员工工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WagyuanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WagyuanListModel::where(['wagid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wagyuan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wagyuan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wagyuan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wagyuan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_wagjian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }


            Db::startTrans();
            try {
                $id = WagjianModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['wagid'] = $id;
                    WagjianListModel::insert($vv);
                }

                (new FlowService())->save_bill('wagjian', $id);

                (new OptService())->relation_file($files,'wagjian',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wagjian', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wagjian', $id, '建造师工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('wagjian');
            $data['flowsetname'] = FlowSetModel::get_name('wagjian');
            $data['isadvan'] = (new FlowService())->check_isadvan('wagjian');
            $data['isnext'] = (new FlowService())->check_isnext('wagjian');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wagjian(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['month'])){
                    return outputJson(4000, '请选择工资月份');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入工资标题');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入工资明细');
                }
            }


            Db::startTrans();
            try {
                WagjianModel::update($param);

                WagjianListModel::where(['wagid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['wagid'] = $param['id'];
                    WagjianListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wagjian', $param['id']);
                }else{
                    (new FlowService())->save_bill('wagjian', $param['id']);
                }

                (new OptService())->relation_file($files,'wagjian',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wagjian', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wagjian', $param['id'], '建造师工资表');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WagjianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WagjianListModel::where(['wagid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wagjian', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wagjian');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wagjian',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wagjian');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['cailei'])){
                    return outputJson(4000, '请选择采购类别');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购明细');
                }
            }



            Db::startTrans();
            try {
                $id = WuCaiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    WuCaiListModel::insert($vv);
                }

                (new FlowService())->save_bill('wu_cai', $id);

                (new OptService())->relation_file($files,'wu_cai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_cai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_cai', $id, '行政采购单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['cailei'] = (new OptService())->get_base_info('wu_cailei');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('wu_cai');
            $data['flowsetname'] = FlowSetModel::get_name('wu_cai');
            $data['isadvan'] = (new FlowService())->check_isadvan('wu_cai');
            $data['isnext'] = (new FlowService())->check_isnext('wu_cai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入采购主题');
                }
                if(empty($param['cailei'])){
                    return outputJson(4000, '请选择采购类别');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入采购明细');
                }
            }


            Db::startTrans();
            try {
                WuCaiModel::update($param);

                WuCaiListModel::where(['caiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    WuCaiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wu_cai', $param['id']);
                }else{
                    (new FlowService())->save_bill('wu_cai', $param['id']);
                }

                (new OptService())->relation_file($files,'wu_cai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_cai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_cai', $param['id'], '行政采购单');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['cailei'] = (new OptService())->get_base_info('wu_cailei');

            $list = WuCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_cai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wu_cai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wu_cai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wu_cai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_wu_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入入库主题');
                }
                if(empty($param['rulei'])){
                    return outputJson(4000, '请选择入库类型');
                }
                if(empty($param['cangku'])){
                    return outputJson(4000, '请选择所属仓库');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入入库人');
                }
            }



            foreach ($param as $k=>$v){
                if(strpos($k,'rufen') !== false){
                    unset($param[$k]);
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入入库明细');
                }
            }



            Db::startTrans();
            try {
                $id = WuRuModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['ruid'] = $id;
                    WuRuListModel::insert($vv);
                }

                (new FlowService())->save_bill('wu_ru', $id);

                (new OptService())->relation_file($files,'wu_ru',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_ru', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_ru', $id, '物品入库');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['rulei'] = (new OptService())->get_base_info('wu_rulei');
            $data['cangku'] = (new OptService())->get_base_info('wu_cangku');
            $data['rufen'] = (new OptService())->get_base_info('wu_rufen');
            $data['baner'] = $this->userinfo['realname'];
            $data['rudt'] = date('Y-m-d');


            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('wu_ru');
            $data['flowsetname'] = FlowSetModel::get_name('wu_ru');
            $data['isadvan'] = (new FlowService())->check_isadvan('wu_ru');
            $data['isnext'] = (new FlowService())->check_isnext('wu_ru');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_ru(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入入库主题');
                }
                if(empty($param['rulei'])){
                    return outputJson(4000, '请选择入库类型');
                }
                if(empty($param['cangku'])){
                    return outputJson(4000, '请选择所属仓库');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入入库人');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'rufen') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入入库明细');
                }
            }


            Db::startTrans();
            try {
                WuRuModel::update($param);

                WuRuListModel::where(['ruid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['ruid'] = $param['id'];
                    WuRuListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wu_ru', $param['id']);
                }else{
                    (new FlowService())->save_bill('wu_ru', $param['id']);
                }

                (new OptService())->relation_file($files,'wu_ru',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_ru', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_ru', $param['id'], '物品入库');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuRuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['rulei'] = (new OptService())->get_base_info('wu_rulei');
            $data['cangku'] = (new OptService())->get_base_info('wu_cangku');
            $data['rufen'] = (new OptService())->get_base_info('wu_rufen');

            $list = WuRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('wu_ru', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wu_ru');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wu_ru',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wu_ru');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入领用主题');
                }
                if(empty($param['cangku'])){
                    return outputJson(4000, '请选择物品仓库');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入领用明细');
                }
            }



            Db::startTrans();
            try {
                $id = WuLingModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['lingid'] = $id;
                    WuLingListModel::insert($vv);
                }

                (new FlowService())->save_bill('wu_ling', $id);

                (new OptService())->relation_file($files,'wu_ling',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'wu_ling', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'wu_ling', $id, '物品领用');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['cangku'] = (new OptService())->get_base_info('wu_cangku');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('wu_ling');
            $data['flowsetname'] = FlowSetModel::get_name('wu_ling');
            $data['isadvan'] = (new FlowService())->check_isadvan('wu_ling');
            $data['isnext'] = (new FlowService())->check_isnext('wu_ling');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_wu_ling(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入领用主题');
                }
                if(empty($param['cangku'])){
                    return outputJson(4000, '请选择物品仓库');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入领用明细');
                }
            }


            Db::startTrans();
            try {
                WuLingModel::update($param);

                WuLingListModel::where(['lingid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['lingid'] = $param['id'];
                    WuLingListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('wu_ling', $param['id']);
                }else{
                    (new FlowService())->save_bill('wu_ling', $param['id']);
                }

                (new OptService())->relation_file($files,'wu_ling',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'wu_ling', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'wu_ling', $param['id'], '物品领用');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = WuLingModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = WuLingListModel::where(['lingid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['cangku_name'] = (new OptService())->get_base_showname('wu_cangku', $item['cangku']);
                $item['rufen_name'] = (new OptService())->get_base_showname('wu_rufen', $item['rufen']);

                $item['runum'] = WuRuListModel::alias('list')
                    ->join('wu_ru ru','ru.id=list.ruid')
                    ->where(['ru.comid'=>$this->userinfo['comid'],'ru.deleted_user'=>0,'ru.isturn'=>1,'ru.status'=>1])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->where(['list.rufen'=>$item['rufen'],'ru.cangku'=>$item['cangku']])
                    ->sum('list.num');

                $item['chunum'] = WuLingListModel::alias('list')
                    ->join('wu_ling ling','ling.id=list.lingid')
                    ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>1])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->where(['list.rufen'=>$item['rufen'],'list.cangku'=>$item['cangku']])
                    ->sum('list.num');

                $item['kucun'] = round($item['runum']-$item['chunum'], 2);

                $item['lingshen'] =  WuLingListModel::alias('list')
                    ->join('wu_ling ling','ling.id=list.lingid')
                    ->where(['ling.comid'=>$this->userinfo['comid'],'ling.deleted_user'=>0,'ling.isturn'=>1,'ling.status'=>0])
                    ->where(['list.name'=>$item['name'],'list.unit'=>$item['unit']])
                    ->where(['list.rufen'=>$item['rufen'],'list.cangku'=>$item['cangku']])
                    ->sum('list.num');

                $item['kechu'] = round($item['runum']-$item['chunum']-$item['lingshen'], 2);
            }
            $data['list'] = $list;

            $data['cangku'] = (new OptService())->get_base_info('wu_cangku');
            $data['files'] = (new OptService())->get_file('wu_ling', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('wu_ling');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('wu_ling',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('wu_ling');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_tongyong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shilei'])){
                    return outputJson(4000, '请选择事项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入事项摘要');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = TongyongModel::insertGetId($param);
                (new FlowService())->save_bill('tongyong', $id);
                (new OptService())->relation_file($files,'tongyong',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tongyong', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tongyong', $id, '通用审批流程');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('tongyong');
            $data['flowsetname'] = FlowSetModel::get_name('tongyong');
            $data['isadvan'] = (new FlowService())->check_isadvan('tongyong');
            $data['isnext'] = (new FlowService())->check_isnext('tongyong');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['shilei'] = (new OptService())->get_base_info('qi_tongyong_shilei');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_tongyong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shilei'])){
                    return outputJson(4000, '请选择事项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入事项摘要');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                TongyongModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('tongyong', $param['id']);
                }else{
                    (new FlowService())->save_bill('tongyong', $param['id']);
                }

                (new OptService())->relation_file($files,'tongyong',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tongyong', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tongyong', $param['id'], '通用审批流程');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TongyongModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['shilei'] = (new OptService())->get_base_info('qi_tongyong_shilei');

            $data['files'] = (new OptService())->get_file('tongyong', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('tongyong');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('tongyong',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('tongyong');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function add_meet(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['room'])){
                    return outputJson(4000, '请选择会议室');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入会议主题');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入发起人');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择预计结束时间');
                }
                if(empty($param['caner'])){
                    return outputJson(4000, '请选择参会人');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = MeetModel::insertGetId($param);
                (new FlowService())->save_bill('meet', $id);
                (new OptService())->relation_file($files,'meet',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'meet', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'meet', $id, '会议');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['baner'] = $this->userinfo['realname'];
            $data['room'] = (new OptService())->get_base_info('qi_meet_room');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('meet');
            $data['flowsetname'] = FlowSetModel::get_name('meet');
            $data['isadvan'] = (new FlowService())->check_isadvan('meet');
            $data['isnext'] = (new FlowService())->check_isnext('meet');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_meet(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['room'])){
                    return outputJson(4000, '请选择会议室');
                }
                if(empty($param['title'])){
                    return outputJson(4000, '请输入会议主题');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入发起人');
                }
                if(empty($param['startdt'])){
                    return outputJson(4000, '请选择开始时间');
                }
                if(empty($param['enddt'])){
                    return outputJson(4000, '请选择预计结束时间');
                }
                if(empty($param['caner'])){
                    return outputJson(4000, '请选择参会人');
                }
            }
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                MeetModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('meet', $param['id']);
                }else{
                    (new FlowService())->save_bill('meet', $param['id']);
                }

                (new OptService())->relation_file($files,'meet',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'meet', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'meet', $param['id'], '会议');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = MeetModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['caner_info'] = (new UserService())->get_users_part_arr($data['data']['caner'], 'user');

            $data['room'] = (new OptService())->get_base_info('qi_meet_room');

            $data['files'] = (new OptService())->get_file('meet', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('meet');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('meet',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('meet');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_qian_suo(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入名称');
                }
                if(empty($param['username'])){
                    return outputJson(4000, '请输入对方姓名');
                }
                if(empty($param['usertel'])){
                    return outputJson(4000, '请输入对方电话');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = QianSuoModel::insertGetId($param);

                (new OptService())->relation_file($files,'qian_suo',$id);
                (new FlowService())->save_bill('qian_suo', $id);



                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'qian_suo', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'qian_suo', $id, '契约锁签章(其他)');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('qian_suo');
            $data['flowsetname'] = FlowSetModel::get_name('qian_suo');
            $data['isadvan'] = (new FlowService())->check_isadvan('qian_suo');
            $data['isnext'] = (new FlowService())->check_isnext('qian_suo');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_qian_suo(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['name'])){
                    return outputJson(4000, '请输入名称');
                }
                if(empty($param['username'])){
                    return outputJson(4000, '请输入对方姓名');
                }
                if(empty($param['usertel'])){
                    return outputJson(4000, '请输入对方电话');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                QianSuoModel::update($param);
                (new OptService())->relation_file($files,'qian_suo',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('qian_suo', $param['id']);
                }else{
                    (new FlowService())->save_bill('qian_suo', $param['id']);
                }

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'qian_suo', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'qian_suo', $param['id'], '契约锁签章(其他)');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = QianSuoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();

            $data['files'] = (new OptService())->get_file('qian_suo', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('qian_suo');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('qian_suo',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('qian_suo');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_notice(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['tongtype'])){
                    return outputJson(4000, '请选择通知类型');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择接收范围');
                }else{
                    if($param['jietype'] != 'all'){
                        if(empty($param['jieer'])){
                            return outputJson(4000, '请选择接收范围的范围值');
                        }
                    }
                }
            }
            $param['facom'] = $this->userinfo['deptid'];
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = NoticeModel::insertGetId($param);
                (new FlowService())->save_bill('notice', $id);
                (new OptService())->relation_file($files,'notice',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'notice', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'notice', $id, '通知公告');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['facom_name'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            $data['tongtype'] = (new XingService())->get_notice_tongtype();
            $data['jietype'] = (new XingService())->get_notice_jietype();
            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['roleArr'] = (new UserService())->get_user_roleArr();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('notice');
            $data['flowsetname'] = FlowSetModel::get_name('notice');
            $data['isadvan'] = (new FlowService())->check_isadvan('notice');
            $data['isnext'] = (new FlowService())->check_isnext('notice');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_notice(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['tongtype'])){
                    return outputJson(4000, '请选择通知类型');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择接收范围');
                }else{
                    if($param['jietype'] != 'all'){
                        if(empty($param['jieer'])){
                            return outputJson(4000, '请选择接收范围的范围值');
                        }
                    }
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                NoticeModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('notice', $param['id']);
                }else{
                    (new FlowService())->save_bill('notice', $param['id']);
                }

                (new OptService())->relation_file($files,'notice',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'notice', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'notice', $param['id'], '通知公告');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = NoticeModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['facom_name'] = DeptModel::get_name_by_pk($data['data']['facom']);


            if($data['data']['jietype'] == 'dept'){
                $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'dept');
            }
            if($data['data']['jietype'] == 'gang'){
                $data['data']['gangid'] = explode(',', $data['data']['jieer']);
            }
            if($data['data']['jietype'] == 'role'){
                $data['data']['roleid'] = explode(',', $data['data']['jieer']);
            }
            if($data['data']['jietype'] == 'user'){
                $data['data']['userid_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'user');
            }


            $data['tongtype'] = (new XingService())->get_notice_tongtype();
            $data['jietype'] = (new XingService())->get_notice_jietype();
            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['roleArr'] = (new UserService())->get_user_roleArr();
            $data['files'] = (new OptService())->get_file('notice', $data['data']['id']);


            $data['files'] = (new OptService())->get_file('notice', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('notice');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('notice',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('notice');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_qianfa(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['wenlei'])){
                    return outputJson(4000, '请选择文件类型');
                }
                if(empty($param['facom'])){
                    return outputJson(4000, '请选择发布部门');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择通知范围');
                }else{
                    if($param['jietype'] != 'all'){
                        if(empty($param['jieer'])){
                            return outputJson(4000, '请选择通知范围的范围值');
                        }
                    }
                }
            }
            $param['facom'] = $this->userinfo['deptid'];
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = QianfaModel::insertGetId($param);
                (new FlowService())->save_bill('qianfa', $id);
                (new OptService())->relation_file($files,'qianfa',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'qianfa', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'qianfa', $id, '文件签发');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['facom_info'] = (new UserService())->get_users_part_arr($this->userinfo['deptid'], 'dept');
            $data['wenlei'] = (new OptService())->get_base_info('qianfa_wenlei');
            $data['jietype'] = (new XingService())->get_qianfa_jietype();
            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['roleArr'] = (new UserService())->get_user_roleArr();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('qianfa');
            $data['flowsetname'] = FlowSetModel::get_name('qianfa');
            $data['isadvan'] = (new FlowService())->check_isadvan('qianfa');
            $data['isnext'] = (new FlowService())->check_isnext('qianfa');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_qianfa(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['title'])){
                    return outputJson(4000, '请输入标题');
                }
                if(empty($param['wenlei'])){
                    return outputJson(4000, '请选择文件类型');
                }
                if(empty($param['facom'])){
                    return outputJson(4000, '请选择发布部门');
                }
                if(empty($param['jietype'])){
                    return outputJson(4000, '请选择通知范围');
                }else{
                    if($param['jietype'] != 'all'){
                        if(empty($param['jieer'])){
                            return outputJson(4000, '请选择通知范围的范围值');
                        }
                    }
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                QianfaModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('qianfa', $param['id']);
                }else{
                    (new FlowService())->save_bill('qianfa', $param['id']);
                }

                (new OptService())->relation_file($files,'qianfa',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'qianfa', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'qianfa', $param['id'], '文件签发');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = QianfaModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['facom_info'] = (new UserService())->get_users_part_arr($data['data']['facom'], 'dept');


            if($data['data']['jietype'] == 'dept'){
                $data['data']['deptid_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'dept');
            }
            if($data['data']['jietype'] == 'gang'){
                $data['data']['gangid'] = explode(',', $data['data']['jieer']);
            }
            if($data['data']['jietype'] == 'role'){
                $data['data']['roleid'] = explode(',', $data['data']['jieer']);
            }
            if($data['data']['jietype'] == 'user'){
                $data['data']['userid_info'] = (new UserService())->get_users_part_arr($data['data']['jieer'], 'user');
            }

            $data['data']['feiliu_info'] = (new UserService())->get_flowrelas_info($data['data']['feiliu']);
            $data['wenlei'] = (new OptService())->get_base_info('qianfa_wenlei');
            $data['jietype'] = (new XingService())->get_qianfa_jietype();
            $data['gangArr'] = (new UserService())->get_usergang_all();
            $data['roleArr'] = (new UserService())->get_user_roleArr();
            $data['files'] = (new OptService())->get_file('qianfa', $data['data']['id']);


            $data['files'] = (new OptService())->get_file('qianfa', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('qianfa');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('qianfa',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('qianfa');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_seal_wai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shi'])){
                    return outputJson(4000, '请输入事项');
                }
                if(empty($param['sealname'])){
                    return outputJson(4000, '请选择外带印章');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = SealWaiModel::insertGetId($param);
                (new FlowService())->save_bill('seal_wai', $id);
                (new OptService())->relation_file($files,'seal_wai',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_wai', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_wai', $id, '印章外带');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('seal_wai');
            $data['flowsetname'] = FlowSetModel::get_name('seal_wai');
            $data['isadvan'] = (new FlowService())->check_isadvan('seal_wai');
            $data['isnext'] = (new FlowService())->check_isnext('seal_wai');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_wai(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['shi'])){
                    return outputJson(4000, '请输入事项');
                }
                if(empty($param['sealname'])){
                    return outputJson(4000, '请选择外带印章');
                }
                if(empty($param['yudt'])){
                    return outputJson(4000, '请选择预计归还日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                SealWaiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('seal_wai', $param['id']);
                }else{
                    (new FlowService())->save_bill('seal_wai', $param['id']);
                }

                (new OptService())->relation_file($files,'seal_wai',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_wai', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_wai', $param['id'], '印章外带');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealWaiModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }


            $data['data']['sealnameArr'] = explode(',', $data['data']['sealname']);
            $data['files'] = (new OptService())->get_file('seal_wai', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('seal_wai');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('seal_wai',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('seal_wai');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_seal_zhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shenlei'])){
                    return outputJson(4000, '请选择申请类型');
                }
                if(empty($param['isbei'])){
                    return outputJson(4000, '请选择是否备案');
                }
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['yinlei'])){
                    return outputJson(4000, '请选择印章类型');
                }
                if(empty($param['yinxing'])){
                    return outputJson(4000, '请选择印章形状');
                }
                if(empty($param['yinnei'])){
                    return outputJson(4000, '请输入印章内容');
                }
                if(empty($param['shenshi'])){
                    return outputJson(4000, '请输入申请事由(使用范围)');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = SealZhiModel::insertGetId($param);
                (new FlowService())->save_bill('seal_zhi', $id);
                (new OptService())->relation_file($files,'seal_zhi',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_zhi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_zhi', $id, '印章刻制');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['baoer'] = $this->userinfo['realname'];
            $data['shenlei'] = (new XingService())->get_sealzhi_shenlei();
            $data['yinlei'] = (new XingService())->get_sealzhi_yinlei();
            $data['yinxing'] = (new XingService())->get_sealzhi_yinxing();
            $data['yinhan'] = (new XingService())->get_sealzhi_yinhan();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('seal_zhi');
            $data['flowsetname'] = FlowSetModel::get_name('seal_zhi');
            $data['isadvan'] = (new FlowService())->check_isadvan('seal_zhi');
            $data['isnext'] = (new FlowService())->check_isnext('seal_zhi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_seal_zhi(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['shenlei'])){
                    return outputJson(4000, '请选择申请类型');
                }
                if(empty($param['isbei'])){
                    return outputJson(4000, '请选择是否备案');
                }
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['yinlei'])){
                    return outputJson(4000, '请选择印章类型');
                }
                if(empty($param['yinxing'])){
                    return outputJson(4000, '请选择印章形状');
                }
                if(empty($param['yinnei'])){
                    return outputJson(4000, '请输入印章内容');
                }
                if(empty($param['shenshi'])){
                    return outputJson(4000, '请输入申请事由(使用范围)');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }

            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);




            Db::startTrans();
            try {
                SealZhiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('seal_zhi', $param['id']);
                }else{
                    (new FlowService())->save_bill('seal_zhi', $param['id']);
                }

                (new OptService())->relation_file($files,'seal_zhi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'seal_zhi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'seal_zhi', $param['id'], '印章刻制');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = SealZhiModel::where(['id'=>$id])->find();
            if(!$data['data'] && $data['data']['isturn'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $data['shenlei'] = (new XingService())->get_sealzhi_shenlei();
            $data['yinlei'] = (new XingService())->get_sealzhi_yinlei();
            $data['yinxing'] = (new XingService())->get_sealzhi_yinxing();
            $data['yinhan'] = (new XingService())->get_sealzhi_yinhan();

            $data['data']['yinleiArr'] = explode(',', $data['data']['yinlei']);
            $data['data']['yinxingArr'] = explode(',', $data['data']['yinxing']);

            $data['files'] = (new OptService())->get_file('seal_zhi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('seal_zhi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('seal_zhi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('seal_zhi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    //##############################################  前期费  ##############################################
    public function add_qian_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'shouqianlei') !== false){
                    unset($param[$k]);
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }


            Db::startTrans();
            try {
                $id = QianShouModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['shouid'] = $id;
                    QianShouListModel::insert($vv);
                }

                (new FlowService())->save_bill('qian_shou', $id);

                (new OptService())->relation_file($files,'qian_shou',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'qian_shou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'qian_shou', $id, '前期费收取-直营');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shouqianlei'] = (new OptService())->get_base_info('cai_shouqianlei');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('qian_shou');
            $data['flowsetname'] = FlowSetModel::get_name('qian_shou');
            $data['isadvan'] = (new FlowService())->check_isadvan('qian_shou');
            $data['isnext'] = (new FlowService())->check_isnext('qian_shou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_qian_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'shouqianlei') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }

            Db::startTrans();
            try {
                QianShouModel::update($param);

                QianShouListModel::where(['shouid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['shouid'] = $param['id'];
                    QianShouListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('qian_shou', $param['id']);
                }else{
                    (new FlowService())->save_bill('qian_shou', $param['id']);
                }

                (new OptService())->relation_file($files,'qian_shou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'qian_shou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'qian_shou', $param['id'], '前期费收取-直营');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = QianShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);


            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $list = QianShouListModel::where(['shouid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shouqianlei'] = (new OptService())->get_base_info('cai_shouqianlei');
            $data['files'] = (new OptService())->get_file('qian_shou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('qian_shou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('qian_shou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('qian_shou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_qian_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['isturn'] > 0){
                if(empty($param['gofuer'])){
                    return outputJson(4000, '请选择工程负责人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'shouqianlei') !== false){
                    unset($param[$k]);
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }


            Db::startTrans();
            try {
                $id = QianBanModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['banid'] = $id;
                    QianBanListModel::insert($vv);
                }

                (new FlowService())->save_bill('qian_ban', $id);

                (new OptService())->relation_file($files,'qian_ban',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'qian_ban', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'qian_ban', $id, '前期费收取-办事处');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shouqianlei'] = (new OptService())->get_base_info('cai_shouqianlei');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('qian_ban');
            $data['flowsetname'] = FlowSetModel::get_name('qian_ban');
            $data['isadvan'] = (new FlowService())->check_isadvan('qian_ban');
            $data['isnext'] = (new FlowService())->check_isnext('qian_ban');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_qian_ban(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['gofuer'])){
                    return outputJson(4000, '请选择工程负责人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'shouqianlei') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入款项明细');
                }
            }

            Db::startTrans();
            try {
                QianBanModel::update($param);

                QianBanListModel::where(['banid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['banid'] = $param['id'];
                    QianBanListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('qian_ban', $param['id']);
                }else{
                    (new FlowService())->save_bill('qian_ban', $param['id']);
                }

                (new OptService())->relation_file($files,'qian_ban',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'qian_ban', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'qian_ban', $param['id'], '前期费收取-办事处');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = QianBanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['gofuerinfo'] = (new BidService())->get_dealcom_info($data['data']['gofuer']);
            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $list = QianBanListModel::where(['banid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shouqianlei'] = (new OptService())->get_base_info('cai_shouqianlei');
            $data['files'] = (new OptService())->get_file('qian_ban', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('qian_ban');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('qian_ban',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('qian_ban');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_qian_zhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['fulei'])){
                    return outputJson(4000, '请选择付款类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'zhiqianlei') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }



            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }


            Db::startTrans();
            try {
                $id = QianZhiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['zhiid'] = $id;
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = QianZhiListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        QianZhiGuanModel::insert([
                            'type' => 1,
                            'zhiid' => $id,
                            'zhilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }




                (new FlowService())->save_bill('qian_zhi', $id);

                (new OptService())->relation_file($files,'qian_zhi',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'qian_zhi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'qian_zhi', $id, '前期费支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $zhiqianlei = (new OptService())->get_base_info('cai_zhiqianlei');
            foreach ($zhiqianlei as $kkk=>$fff){
                if(in_array($fff['name'], ['投标保证金缴纳'])){
                    unset($zhiqianlei[$kkk]);
                }
            }
            sort($zhiqianlei);
            $data['zhiqianlei'] = $zhiqianlei;

            $data['fulei'] = (new OptService())->get_base_info('cai_qianzhifulei');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('qian_zhi');
            $data['flowsetname'] = FlowSetModel::get_name('qian_zhi');
            $data['isadvan'] = (new FlowService())->check_isadvan('qian_zhi');
            $data['isnext'] = (new FlowService())->check_isnext('qian_zhi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_qian_zhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['fulei'])){
                    return outputJson(4000, '请选择付款类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'zhiqianlei') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }

            Db::startTrans();
            try {
                QianZhiModel::update($param);

                QianZhiListModel::where(['zhiid'=>$param['id']])->delete();
                QianZhiGuanModel::where(['zhiid'=>$param['id'],'type' => 1])->delete();
                foreach($list as $vv){
                    $vv['zhiid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = QianZhiListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        QianZhiGuanModel::insert([
                            'type' => 1,
                            'zhiid' => $param['id'],
                            'zhilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('qian_zhi', $param['id']);
                }else{
                    (new FlowService())->save_bill('qian_zhi', $param['id']);
                }

                (new OptService())->relation_file($files,'qian_zhi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'qian_zhi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'qian_zhi', $param['id'], '前期费支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = QianZhiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid'], $data['data']['id']);
            $list = QianZhiListModel::where(['zhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $difeeList = QianZhiGuanModel::field('piaoid,piaolistid,difee')
                    ->where(['type'=>1,'zhiid'=>$item['zhiid'],'zhilistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;

            $zhiqianlei = (new OptService())->get_base_info('cai_zhiqianlei');
            foreach ($zhiqianlei as $kkk=>$fff){
                if(in_array($fff['name'], ['投标保证金缴纳'])){
                    unset($zhiqianlei[$kkk]);
                }
            }
            sort($zhiqianlei);
            $data['zhiqianlei'] = $zhiqianlei;

            $data['fulei'] = (new OptService())->get_base_info('cai_qianzhifulei');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('qian_zhi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('qian_zhi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('qian_zhi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('qian_zhi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_qian_piao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进项票明细');
                }

                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = QianPiaoListModel::alias('list')
                        ->join('qian_piao piao','piao.id=list.piaoid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }


            Db::startTrans();
            try {
                $id = QianPiaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['piaoid'] = $id;
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = QianPiaoListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        QianZhiGuanModel::insert([
                            'type' => 2,
                            'zhiid' => $vvvv['zhiid'],
                            'zhilistid' => $vvvv['zhilistid'],
                            'piaoid' => $id,
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }

                (new FlowService())->save_bill('qian_piao', $id);

                (new OptService())->relation_file($files,'qian_piao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'qian_piao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'qian_piao', $id, '前期费进项票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('qian_piao');
            $data['flowsetname'] = FlowSetModel::get_name('qian_piao');
            $data['isadvan'] = (new FlowService())->check_isadvan('qian_piao');
            $data['isnext'] = (new FlowService())->check_isnext('qian_piao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_qian_piao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入进项票明细');
                }


                $kknum = 1;
                foreach ($list as $bbb){
                    $cunzai = QianPiaoListModel::alias('list')
                        ->join('qian_piao piao','piao.id=list.piaoid')
                        ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                        ->where('piao.status','in',[0,1])
                        ->where('piao.id','neq',$param['id'])
                        ->where(['list.nums'=>$bbb['nums']])
                        ->find();

                    if($cunzai){
                        return outputJson(4000, "第".$kknum."行发票号码已存在");
                    }
                    $kknum++;
                }
            }

            Db::startTrans();
            try {
                QianPiaoModel::update($param);

                QianPiaoListModel::where(['piaoid'=>$param['id']])->delete();
                QianZhiGuanModel::where(['piaoid'=>$param['id'],'type' => 2])->delete();
                foreach($list as $vv){
                    $vv['piaoid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = QianPiaoListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        QianZhiGuanModel::insert([
                            'type' => 2,
                            'zhiid' => $vvvv['zhiid'],
                            'zhilistid' => $vvvv['zhilistid'],
                            'piaoid' => $param['id'],
                            'piaolistid' => $listid,
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('qian_piao', $param['id']);
                }else{
                    (new FlowService())->save_bill('qian_piao', $param['id']);
                }

                (new OptService())->relation_file($files,'qian_piao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'qian_piao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'qian_piao', $param['id'], '前期费进项票');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = QianPiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);
            $list = QianPiaoListModel::where(['piaoid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $difeeList = QianZhiGuanModel::field('zhiid,zhilistid,difee')
                    ->where(['type'=>2,'piaoid'=>$item['piaoid'],'piaolistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;

            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');
            $data['files'] = (new OptService())->get_file('qian_piao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('qian_piao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('qian_piao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('qian_piao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_account_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['chuid'])){
                    return outputJson(4000, '请选择调出(付款)账户');
                }
                if(empty($param['ruid'])){
                    return outputJson(4000, '请选择调入(收款)账户');
                }
                if($param['chuid'] == $param['ruid']){
                    return outputJson(4000, '调出账户不能与调入账户相同');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入转账金额');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择交易日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }
            }


            $param['zhiqilei'] = 11;
            $param['shouqilei'] = 5;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = AccountDiaoModel::insertGetId($param);

                (new FlowService())->save_bill('account_diao', $id);

                (new OptService())->relation_file($files,'account_diao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_diao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_diao', $id, '资金调拨');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('account_diao');
            $data['flowsetname'] = FlowSetModel::get_name('account_diao');
            $data['isadvan'] = (new FlowService())->check_isadvan('account_diao');
            $data['isnext'] = (new FlowService())->check_isnext('account_diao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account_diao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['chuid'])){
                    return outputJson(4000, '请选择调出(付款)账户');
                }
                if(empty($param['ruid'])){
                    return outputJson(4000, '请选择调入(收款)账户');
                }
                if($param['chuid'] == $param['ruid']){
                    return outputJson(4000, '调出账户不能与调入账户相同');
                }
                if(empty($param['money'])){
                    return outputJson(4000, '请输入转账金额');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择交易日期');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            Db::startTrans();
            try {
                AccountDiaoModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('account_diao', $param['id']);
                }else{
                    (new FlowService())->save_bill('account_diao', $param['id']);
                }

                (new OptService())->relation_file($files,'account_diao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_diao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_diao', $param['id'], '资金调拨');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountDiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $chuaccount = AccountModel::get_info_by_pk($data['data']['chuid']);
            $data['chuaccount'] = $chuaccount;

            $ruaccount = AccountModel::get_info_by_pk($data['data']['ruid']);
            $data['ruaccount'] = $ruaccount;

            $data['files'] = (new OptService())->get_file('account_diao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('account_diao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('account_diao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('account_diao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_tou_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['ziqu'])){
                    return outputJson(4000, '请选择资金渠道');
                }
            }

            if($param['ziqu'] != 1){
                $param['shouid'] = 0;
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }


            //投标保证金缴纳，垫资月息，仅当 资金渠道为 公司垫资 时，默认是2，其他情况，设置展示为0
            if($param['ziqu'] != 2){
                $param['dianrate'] = 0;
            }


            $param['zhiqilei'] = 7;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入支付明细');
                }
            }


            Db::startTrans();
            try {
                $id = TouJiaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jiaoid'] = $id;
                    TouJiaoListModel::insert($vv);
                }

                (new FlowService())->save_bill('tou_jiao', $id);

                (new OptService())->relation_file($files,'tou_jiao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tou_jiao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tou_jiao', $id, '投标保证金缴纳');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['ziqu'] = (new QianService())->get_tou_jiao_ziqu();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('tou_jiao');
            $data['flowsetname'] = FlowSetModel::get_name('tou_jiao');
            $data['isadvan'] = (new FlowService())->check_isadvan('tou_jiao');
            $data['isnext'] = (new FlowService())->check_isnext('tou_jiao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_tou_jiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['ziqu'])){
                    return outputJson(4000, '请选择资金渠道');
                }
            }

            if($param['ziqu'] != 1){
                $param['shouid'] = 0;
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }

            //投标保证金缴纳，垫资月息，仅当 资金渠道为 公司垫资 时，默认是2，其他情况，设置展示为0
            if($param['ziqu'] != 2){
                $param['dianrate'] = 0;
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入支付明细');
                }
            }

            Db::startTrans();
            try {
                TouJiaoModel::update($param);

                TouJiaoListModel::where(['jiaoid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jiaoid'] = $param['id'];
                    TouJiaoListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('tou_jiao', $param['id']);
                }else{
                    (new FlowService())->save_bill('tou_jiao', $param['id']);
                }

                (new OptService())->relation_file($files,'tou_jiao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'tou_jiao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'tou_jiao', $param['id'], '投标保证金缴纳');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TouJiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);
            $data['shouidinfo'] = QianShouModel::where(['id'=>$data['data']['shouid']])->find();
            $list = TouJiaoListModel::where(['jiaoid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['ziqu'] = (new QianService())->get_tou_jiao_ziqu();
            $data['files'] = (new OptService())->get_file('tou_jiao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('tou_jiao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('tou_jiao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('tou_jiao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_tou_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['jiaoid'])){
                    return outputJson(4000, '请选择关联保证金缴纳');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择收款对象');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }


            $param['qishoulei'] = 1;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = TouFanModel::insertGetId($param);

                (new FlowService())->save_bill('tou_fan', $id);

                (new OptService())->relation_file($files,'tou_fan',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tou_fan', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tou_fan', $id, '投标保证金返还');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('tou_fan');
            $data['flowsetname'] = FlowSetModel::get_name('tou_fan');
            $data['isadvan'] = (new FlowService())->check_isadvan('tou_fan');
            $data['isnext'] = (new FlowService())->check_isnext('tou_fan');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_tou_fan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['jiaoid'])){
                    return outputJson(4000, '请选择关联保证金缴纳');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请选择收款对象');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                TouFanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('tou_fan', $param['id']);
                }else{
                    (new FlowService())->save_bill('tou_fan', $param['id']);
                }

                (new OptService())->relation_file($files,'tou_fan',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'tou_fan', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'tou_fan', $param['id'], '投标保证金返还');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TouFanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);
            $data['jiaoidinfo'] = TouJiaoModel::where(['id'=>$data['data']['jiaoid']])->find();
            if($data['jiaoidinfo']){
                $data['jiaoidinfo']['jiaosericnum'] = FlowBillModel::where(['modenum'=>'tou_jiao','mid'=>$data['jiaoidinfo']['id']])->value('sericnum');
            }
            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['files'] = (new OptService())->get_file('tou_fan', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('tou_fan');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('tou_fan',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('tou_fan');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_tou_han(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入保函名称');
                }
                if(empty($param['kaigou'])){
                    return outputJson(4000, '请输入开具机构');
                }
                if(empty($param['baotai'])){
                    return outputJson(4000, '请选择保函状态');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = TouHanModel::insertGetId($param);

                (new FlowService())->save_bill('tou_han', $id);

                (new OptService())->relation_file($files,'tou_han',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'tou_han', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'tou_han', $id, '投标保函登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['baotai'] = (new OptService())->get_base_info('cai_baotai');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('tou_han');
            $data['flowsetname'] = FlowSetModel::get_name('tou_han');
            $data['isadvan'] = (new FlowService())->check_isadvan('tou_han');
            $data['isnext'] = (new FlowService())->check_isnext('tou_han');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_tou_han(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['bidid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['name'])){
                    return outputJson(4000, '请输入保函名称');
                }
                if(empty($param['kaigou'])){
                    return outputJson(4000, '请输入开具机构');
                }
                if(empty($param['baotai'])){
                    return outputJson(4000, '请选择保函状态');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                TouHanModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('tou_han', $param['id']);
                }else{
                    (new FlowService())->save_bill('tou_han', $param['id']);
                }

                (new OptService())->relation_file($files,'tou_han',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'tou_han', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'tou_han', $param['id'], '投标保函登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = TouHanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['bidinfo'] = (new BidService())->get_bid_info($data['data']['bidid']);


            $data['baotai'] = (new OptService())->get_base_info('cai_baotai');
            $data['files'] = (new OptService())->get_file('tou_han', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('tou_han');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('tou_han',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('tou_han');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function add_hou_chushui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }



            $param['zhiqilei'] = 3;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }


            Db::startTrans();
            try {
                $id = HouChushuiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['chushuiid'] = $id;
                    HouChushuiListModel::insert($vv);
                }

                (new FlowService())->save_bill('hou_chushui', $id);

                (new OptService())->relation_file($files,'hou_chushui',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_chushui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_chushui', $id, '储备税费支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['yiju'] = (new HouService())->get_hou_chushui_yiju();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_chushui');
            $data['flowsetname'] = FlowSetModel::get_name('hou_chushui');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_chushui');
            $data['isnext'] = (new FlowService())->check_isnext('hou_chushui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_chushui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }

            Db::startTrans();
            try {
                HouChushuiModel::update($param);

                HouChushuiListModel::where(['chushuiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['chushuiid'] = $param['id'];
                    HouChushuiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_chushui', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_chushui', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_chushui',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_chushui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_chushui', $param['id'], '储备税费支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouChushuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['liantel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'lianer'=>$data['data']['lianer']])->value('liantel');

            $list = HouChushuiListModel::where(['chushuiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['lei_kuan'] = HouChujinListModel::alias('list')
                    ->join('hou_chujin hou','hou.id=list.chujinid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.fuer'=>$item['fuer']])
                    ->sum('list.money');
                $item['lei_piao'] = PiaoChuListModel::alias('list')
                    ->join('piao_chu hou','hou.id=list.piaochuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.xiaoer'=>$item['fuer']])
                    ->sum('list.money');
                $item['lei_shui'] = HouChushuiListModel::alias('list')
                    ->join('hou_chushui hou','hou.id=list.chushuiid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.fuer'=>$item['fuer']])
                    ->sum('list.money');
            }
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['yiju'] = (new HouService())->get_hou_chushui_yiju();

            $data['files'] = (new OptService())->get_file('hou_chushui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_chushui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_chushui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_chushui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_hou_chujin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }



            $param['zhiqilei'] = 2;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }


            Db::startTrans();
            try {
                $id = HouChujinModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['chujinid'] = $id;
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = HouChujinListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouChujinGuanModel::insert([
                            'type' => 1,
                            'chujinid' => $id,
                            'chujinlistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }

                (new FlowService())->save_bill('hou_chujin', $id);

                (new OptService())->relation_file($files,'hou_chujin',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_chujin', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_chujin', $id, '储备进项支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_chujin');
            $data['flowsetname'] = FlowSetModel::get_name('hou_chujin');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_chujin');
            $data['isnext'] = (new FlowService())->check_isnext('hou_chujin');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_chujin(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['lianer'])){
                    return outputJson(4000, '请选择销售方联系人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }



            Db::startTrans();
            try {
                HouChujinModel::update($param);

                HouChujinListModel::where(['chujinid'=>$param['id']])->delete();
                HouChujinGuanModel::where(['chujinid'=>$param['id'],'type' => 1])->delete();
                foreach($list as $vv){
                    $vv['chujinid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = HouChujinListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouChujinGuanModel::insert([
                            'type' => 1,
                            'chujinid' => $param['id'],
                            'chujinlistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }





                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_chujin', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_chujin', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_chujin',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_chujin', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_chujin', $param['id'], '储备进项支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouChujinModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['liantel'] = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'status'=>1,'lianer'=>$data['data']['lianer']])->value('liantel');

            $list = HouChujinListModel::where(['chujinid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['chuid_name'] = '';
                $chuInfo = ContractChuModel::where(['id'=>$item['chuid']])->find();
                if($chuInfo){
                    $item['chuid_name'] = $chuInfo['name'];
                    $item['lei_kuan'] = HouChujinListModel::alias('list')
                        ->join('hou_chujin hou','hou.id=list.chujinid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.chuid'=>$item['chuid']])
                        ->sum('list.money');
                    $item['lei_piao'] = PiaoChuListModel::alias('list')
                        ->join('piao_chu hou','hou.id=list.piaochuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.chuid'=>$item['chuid']])
                        ->sum('list.money');
                    $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                        ->where(['isturn'=>1,'status'=>1,'id'=>$item['chuid']])
                        ->sum('money');
                }else{
                    $item['lei_kuan'] = HouChujinListModel::alias('list')
                        ->join('hou_chujin hou','hou.id=list.chujinid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.fuer'=>$item['fuer']])
                        ->sum('list.money');
                    $item['lei_piao'] = PiaoChuListModel::alias('list')
                        ->join('piao_chu hou','hou.id=list.piaochuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.xiaoer'=>$item['fuer']])
                        ->sum('list.money');
                    $item['lei_chumoney'] = ContractChuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                        ->where(['isturn'=>1,'status'=>1,'gonger'=>$item['fuer']])
                        ->sum('money');
                }

                $difeeList = HouChujinGuanModel::field('piaoid,piaolistid,difee')
                    ->where(['type'=>1,'chujinid'=>$item['chujinid'],'chujinlistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);

            }
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('hou_chujin', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_chujin');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_chujin',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_chujin');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function add_hou_gongshui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['rendt'])){
                    return outputJson(4000, '请选择认证日期');
                }
                if(empty($param['tuifang'])){
                    return outputJson(4000, '请选择退税方式');
                }
            }

            $param['zhiqilei'] = 1;
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);


            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }


            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            $fu = array_filter($param['fu']);
            unset($param['fu']);

            if($param['tuifang'] != 1){
                $fu = [];
            }


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入抵扣明细');
                }
            }



            Db::startTrans();
            try {
                $id = HouGongshuiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['gongshuiid'] = $id;
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = HouGongshuiListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouGongshuiGuanModel::insert([
                            'type' => 1,
                            'gongshuiid' => $id,
                            'gongshuilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                        ]);
                    }
                }

                foreach($fu as $mmmm){
                    $mmmm['gongshuiid'] = $id;
                    HouGongshuiFuModel::insert($mmmm);
                }



                (new FlowService())->save_bill('hou_gongshui', $id);

                (new OptService())->relation_file($files,'hou_gongshui',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_gongshui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_gongshui', $id, '进项税额抵扣退税');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);


            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['tuifang'] = (new HouService())->get_hou_gongshui_tuifang();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_gongshui');
            $data['flowsetname'] = FlowSetModel::get_name('hou_gongshui');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_gongshui');
            $data['isnext'] = (new FlowService())->check_isnext('hou_gongshui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_gongshui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['rendt'])){
                    return outputJson(4000, '请选择认证日期');
                }
                if(empty($param['tuifang'])){
                    return outputJson(4000, '请选择退税方式');
                }

            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            $fu = array_filter($param['fu']);
            unset($param['fu']);

            if($param['tuifang'] != 1){
                $fu = [];
            }

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入抵扣明细');
                }
            }

            Db::startTrans();
            try {
                HouGongshuiModel::update($param);

                HouGongshuiListModel::where(['gongshuiid'=>$param['id']])->delete();
                HouGongshuiGuanModel::where(['gongshuiid'=>$param['id'],'type' => 1])->delete();
                foreach($list as $vv){
                    $vv['gongshuiid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);
                    $listid = HouGongshuiListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouGongshuiGuanModel::insert([
                            'type' => 1,
                            'gongshuiid' => $param['id'],
                            'gongshuilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                        ]);
                    }
                }

                HouGongshuiFuModel::where(['gongshuiid'=>$param['id']])->delete();
                foreach($fu as $mmmm){
                    $mmmm['gongshuiid'] = $param['id'];
                    HouGongshuiFuModel::insert($mmmm);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_gongshui', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_gongshui', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_gongshui',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_gongshui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_gongshui', $param['id'], '进项税额抵扣退税');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouGongshuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);


            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['tuifang'] = (new HouService())->get_hou_gongshui_tuifang();

            $list = HouGongshuiListModel::where(['gongshuiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['gongzhifee'] = HouGongzhiListModel::alias('list')
                    ->join('hou_gongzhi hou','hou.id=list.gongzhiid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['hou.enginid'=>$data['data']['enginid'],'list.fuer'=>$item['xiaoer']])
                    ->sum('list.money');
                $item['piaoshoufee'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1])
                    ->where(['shou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['xiaoer']])
                    ->sum('list.money');

                $item['concaimoney'] = (new HouService())->get_contract_cai_final_money($data['data']['enginid'],$item['xiaoer']);

                $item['kedifee'] = 0;
                if($item['gongzhifee'] > 0 && $item['piaoshoufee'] > 0){
                    $item['kedifee'] = $item['gongzhifee'] > $item['piaoshoufee'] ? $item['piaoshoufee'] : $item['gongzhifee'];
                }

                $item['yidimoney'] = HouGongshuiListModel::alias('list')
                    ->join('hou_gongshui hou','hou.id=list.gongshuiid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['hou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['xiaoer']])
                    ->sum('list.money');
                $item['yidiratefee'] = HouGongshuiListModel::alias('list')
                    ->join('hou_gongshui hou','hou.id=list.gongshuiid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['hou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['xiaoer']])
                    ->sum('list.ratefee');

                $item['shengdifee'] = round($item['kedifee'] - $item['yidimoney'], 2);
                $item['shengdifee'] = $item['shengdifee'] > 0 ? $item['shengdifee'] : 0;


                $difeeList = HouGongshuiGuanModel::field('piaoid,piaolistid')
                    ->where(['type'=>1,'gongshuiid'=>$item['gongshuiid'],'gongshuilistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;



            $fu = HouGongshuiFuModel::where(['gongshuiid'=>$data['data']['id']])->select()->toArray();
            $data['fu'] = $fu;


            $data['files'] = (new OptService())->get_file('hou_gongshui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_gongshui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_gongshui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_gongshui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_hou_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['ispiao'])){
                    return outputJson(4000, '请选择有无发票');
                }
                if(empty($param['fuer'])){
                    return outputJson(4000, '请选择付款对象');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['banknum'])){
                    return outputJson(4000, '请选择账户账号');
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'baokuanlei') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }




            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            $list_piao = array_filter($param['list_piao']);
            unset($param['list_piao']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
                if($param['ispiao'] == '有'){
                    if(count($list_piao) < 1){
                        return outputJson(4000, '请输入发票登记信息');
                    }
                }else{
                    $list_piao = [];
                }
            }


//            if($param['isturn'] > 0){
//                $wangPiaofee = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('piaofee');
//                $wangMoney = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
//                    ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('money');
//                $piaosheng = $wangPiaofee - $wangMoney + $param['piaofee'];
//                if( $piaosheng < $param['money']){
//                    return outputJson(4000, '可用发票金额不足,请修改');
//                }
//            }




            if($param['isturn'] > 0){
                if(count($list_piao) > 0){
                    $kknum = 1;
                    foreach ($list_piao as $bbb){
                        $cunzai = HouBaoPiaoModel::alias('list')
                            ->join('hou_bao piao','piao.id=list.baoid')
                            ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                            ->where('piao.status','in',[0,1])
                            ->where(['list.nums'=>$bbb['nums']])
                            ->find();

                        if($cunzai){
                            return outputJson(4000, "第".$kknum."行发票号码已存在");
                        }
                        $kknum++;
                    }
                }
            }



            Db::startTrans();
            try {
                $id = HouBaoModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['baoid'] = $id;
                    HouBaoListModel::insert($vv);
                }

                foreach($list_piao as $vvv){
                    $vvv['baoid'] = $id;
                    HouBaoPiaoModel::insert($vvv);
                }


                (new FlowService())->save_bill('hou_bao', $id);

                (new OptService())->relation_file($files,'hou_bao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_bao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_bao', $id, '费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $wangPiaofee = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('piaofee');
            $wangMoney = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('money');

            $data['piaosheng'] = round($wangPiaofee - $wangMoney);

            $data['shanginfo'] = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'created_user'=>$this->userinfo['id']])
                ->where(['isturn'=>1,'status'=>1])->order('id DESC')->find();

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['baokuanlei'] = (new OptService())->get_base_info('cai_baokuanlei');
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_bao');
            $data['flowsetname'] = FlowSetModel::get_name('hou_bao');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_bao');
            $data['isnext'] = (new FlowService())->check_isnext('hou_bao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['ispiao'])){
                    return outputJson(4000, '请选择有无发票');
                }
                if(empty($param['fuer'])){
                    return outputJson(4000, '请选择付款对象');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择支付方式');
                }
                if(empty($param['banknum'])){
                    return outputJson(4000, '请选择账户账号');
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'baokuanlei') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'qianpiaotype') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            $list_piao = array_filter($param['list_piao']);
            unset($param['list_piao']);

            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入报销明细');
                }
                if($param['ispiao'] == '有'){
                    if(count($list_piao) < 1){
                        return outputJson(4000, '请输入发票登记信息');
                    }
                }else{
                    $list_piao = [];
                }
            }

//            $wangPiaofee = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->where('id','neq',$param['id'])
//                ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('piaofee');
//            $wangMoney = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->where('id','neq',$param['id'])
//                ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('money');
//            $piaosheng = $wangPiaofee - $wangMoney + $param['piaofee'];
//            if( $piaosheng < $param['money']){
//                return outputJson(4000, '可用发票金额不足,请修改');
//            }


            if($param['isturn'] > 0){
                if(count($list_piao) > 0){
                    $kknum = 1;
                    foreach ($list_piao as $bbb){
                        $cunzai = HouBaoPiaoModel::alias('list')
                            ->join('hou_bao piao','piao.id=list.baoid')
                            ->where(['piao.comid'=>$this->userinfo['comid'],'piao.deleted_user'=>0,'piao.isturn'=>1])
                            ->where('piao.status','in',[0,1])
                            ->where('piao.id','neq',$param['id'])
                            ->where(['list.nums'=>$bbb['nums']])
                            ->find();

                        if($cunzai){
                            return outputJson(4000, "第".$kknum."行发票号码已存在");
                        }
                        $kknum++;
                    }
                }
            }


            Db::startTrans();
            try {
                HouBaoModel::update($param);

                HouBaoListModel::where(['baoid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['baoid'] = $param['id'];
                    HouBaoListModel::insert($vv);
                }

                HouBaoPiaoModel::where(['baoid'=>$param['id']])->delete();
                foreach($list_piao as $vvv){
                    $vvv['baoid'] = $param['id'];
                    HouBaoPiaoModel::insert($vvv);
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_bao', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_bao', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_bao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_bao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_bao', $param['id'], '费用报销');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouBaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $wangPiaofee = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->where('id','neq',$data['data']['id'])
                ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('piaofee');
            $wangMoney = HouBaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->where('id','neq',$data['data']['id'])
                ->where(['isturn'=>1,'created_user'=>$this->userinfo['id']])->where('status','in',[0,1])->sum('money');
            $data['piaosheng'] = round($wangPiaofee - $wangMoney);


            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['baokuanlei'] = (new OptService())->get_base_info('cai_baokuanlei');
            $data['qianpiaotype'] = (new OptService())->get_base_info('cai_qianpiaotype');

            $list = HouBaoListModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['baokuanlei_name'] = (new OptService())->get_base_showname('cai_baokuanlei', $item['baokuanlei']);
            }
            $data['list'] = $list;


            $list_piao = HouBaoPiaoModel::where(['baoid'=>$data['data']['id']])->select()->toArray();
            foreach ($list_piao as &$mmm){
                $mmm['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $mmm['qianpiaotype']);
            }
            $data['list_piao'] = $list_piao;


            $data['files'] = (new OptService())->get_file('hou_bao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_bao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_bao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_bao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_hou_gongzhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['fulei'])){
                    return outputJson(4000, '请选择付款类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'zhikuanlei') !== false){
                    unset($param[$k]);
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }


            Db::startTrans();
            try {
                $id = HouGongzhiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['gongzhiid'] = $id;

                    $quobj = json_decode($vv['quobj'], true) ?: [];
                    unset($vv['quobj']);

                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);

                    $listid = HouGongzhiListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouGongzhiGuanModel::insert([
                            'type' => 1,
                            'gongzhiid' => $id,
                            'gongzhilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }

                    foreach($quobj as $quvv){
                        HouGongzhiQuModel::insert([
                            'type' => $quvv['type'],
                            'qienid' => $quvv['qienid'],
                            'money' => $quvv['money'],
                            'gongzhiid' => $id,
                            'gongzhilistid' => $listid,
                        ]);
                    }

                }

                (new FlowService())->save_bill('hou_gongzhi', $id);

                (new OptService())->relation_file($files,'hou_gongzhi',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_gongzhi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_gongzhi', $id, '工程支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);
            $data['last_kou_list'] = (new HouService())->get_engin_last_kou_list($enginid);


            $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['fulei'] = (new HouService())->get_hou_gongzhi_fulei();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_gongzhi');
            $data['flowsetname'] = FlowSetModel::get_name('hou_gongzhi');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_gongzhi');
            $data['isnext'] = (new FlowService())->check_isnext('hou_gongzhi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_gongzhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['fulei'])){
                    return outputJson(4000, '请选择付款类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'zhikuanlei') !== false){
                    unset($param[$k]);
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }








            Db::startTrans();
            try {
                HouGongzhiModel::update($param);

                HouGongzhiListModel::where(['gongzhiid'=>$param['id']])->delete();
                HouGongzhiGuanModel::where(['gongzhiid'=>$param['id']])->delete();
                HouGongzhiQuModel::where(['gongzhiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gongzhiid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);

                    $quobj = json_decode($vv['quobj'], true) ?: [];
                    unset($vv['quobj']);

                    $listid = HouGongzhiListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouGongzhiGuanModel::insert([
                            'type' => 1,
                            'gongzhiid' => $param['id'],
                            'gongzhilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }


                    foreach($quobj as $quvv){
                        HouGongzhiQuModel::insert([
                            'type' => $quvv['type'],
                            'qienid' => $quvv['qienid'],
                            'money' => $quvv['money'],
                            'gongzhiid' => $param['id'],
                            'gongzhilistid' => $listid,
                        ]);
                    }
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_gongzhi', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_gongzhi', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_gongzhi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_gongzhi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_gongzhi', $param['id'], '工程支付');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouGongzhiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['zhikuanlei'] = (new OptService())->get_base_info('cai_zhikuanlei');
            $data['fulei'] = (new HouService())->get_hou_gongzhi_fulei();

            $list = HouGongzhiListModel::where(['gongzhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');

                $item['gongzhifee'] = HouGongzhiListModel::alias('list')
                    ->join('hou_gongzhi hou','hou.id=list.gongzhiid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['hou.enginid'=>$data['data']['enginid'],'list.fuer'=>$item['fuer']])
                    ->sum('list.money');
                $item['piaoshoufee'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou shou','shou.id=list.shouid')
                    ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0,'shou.isturn'=>1,'shou.status'=>1])
                    ->where(['shou.enginid'=>$data['data']['enginid'],'list.xiaoer'=>$item['fuer']])
                    ->sum('list.money');

                $item['concaimoney'] = (new HouService())->get_contract_cai_final_money($data['data']['enginid'],$item['fuer']);

                $difeeList = HouGongzhiGuanModel::field('piaoid,piaolistid,difee')
                    ->where(['type'=>1,'gongzhiid'=>$item['gongzhiid'],'gongzhilistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);


                $quList = HouGongzhiQuModel::field('type,qienid,money')
                    ->where(['gongzhiid'=>$item['gongzhiid'],'gongzhilistid'=>$item['id']])->select()->toArray();
                $item['quobj'] = json_encode($quList);
            }
            $data['list'] = $list;

            $data['last_kou_list'] = (new HouService())->get_engin_last_kou_list($data['data']['enginid']);

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['files'] = (new OptService())->get_file('hou_gongzhi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_gongzhi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_gongzhi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_gongzhi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_hou_dayzhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['feilei'])){
                    return outputJson(4000, '请选择费用类别');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'zhiqilei') !== false){
                    unset($param[$k]);
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }


            Db::startTrans();
            try {
                $id = HouDayzhiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['dayzhiid'] = $id;
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);

                    $listid = HouDayzhiListModel::insertGetId($vv);

                    foreach($difeeobj as $vvvv){
                        HouDayzhiGuanModel::insert([
                            'type' => 1,
                            'dayzhiid' => $id,
                            'dayzhilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }

                (new FlowService())->save_bill('hou_dayzhi', $id);

                (new OptService())->relation_file($files,'hou_dayzhi',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_dayzhi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_dayzhi', $id, '日常付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['feilei'] = (new OptService())->get_base_info('cai_dayzhifeilei');
            $data['zhiqilei'] = (new OptService())->get_base_info('cai_zhiqilei',2);
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_dayzhi');
            $data['flowsetname'] = FlowSetModel::get_name('hou_dayzhi');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_dayzhi');
            $data['isnext'] = (new FlowService())->check_isnext('hou_dayzhi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_dayzhi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['feilei'])){
                    return outputJson(4000, '请选择费用类别');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
            }

            foreach ($param as $k=>$v){
                if(strpos($k,'incdaotype') !== false){
                    unset($param[$k]);
                }
            }
            foreach ($param as $k=>$v){
                if(strpos($k,'zhiqilei') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入付款明细');
                }
            }








            Db::startTrans();
            try {
                HouDayzhiModel::update($param);

                HouDayzhiListModel::where(['dayzhiid'=>$param['id']])->delete();
                HouDayzhiGuanModel::where(['dayzhiid'=>$param['id'],'type' => 1])->delete();
                foreach($list as $vv){
                    $vv['dayzhiid'] = $param['id'];
                    $difeeobj = json_decode($vv['difeeobj'], true) ?: [];
                    unset($vv['difeeobj']);

                    $listid = HouDayzhiListModel::insertGetId($vv);
                    foreach($difeeobj as $vvvv){
                        HouDayzhiGuanModel::insert([
                            'type' => 1,
                            'dayzhiid' => $param['id'],
                            'dayzhilistid' => $listid,
                            'piaoid' => $vvvv['piaoid'],
                            'piaolistid' => $vvvv['piaolistid'],
                            'difee' => $vvvv['difee'],
                        ]);
                    }
                }


                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_dayzhi', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_dayzhi', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_dayzhi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_dayzhi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_dayzhi', $param['id'], '日常付款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouDayzhiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['fu_unit_info'] = (new UserService())->get_users_part_arr($data['data']['fu_unit'], 'dept');

            $data['feilei'] = (new OptService())->get_base_info('cai_dayzhifeilei');
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['zhiqilei'] = (new OptService())->get_base_info('cai_zhiqilei', 2);

            $list = HouDayzhiListModel::where(['dayzhiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $difeeList = HouDayzhiGuanModel::field('piaoid,piaolistid,difee')
                    ->where(['type'=>1,'dayzhiid'=>$item['dayzhiid'],'dayzhilistid'=>$item['id']])->select()->toArray();
                $item['difeeobj'] = json_encode($difeeList);
            }
            $data['list'] = $list;


            $data['files'] = (new OptService())->get_file('hou_dayzhi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_dayzhi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_dayzhi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_dayzhi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_hou_gonghui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['shougonglei'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
                if(empty($param['iskou'])){
                    return outputJson(4000, '请选择本次回款是否收取');
                }
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                $id = HouGonghuiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['gonghuiid'] = $id;
                    $vv['type'] = 1;
                    HouGonghuiListModel::insert($vv);
                }

                (new FlowService())->save_bill('hou_gonghui', $id);

                (new OptService())->relation_file($files,'hou_gonghui',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_gonghui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_gonghui', $id, '工程回款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shougonglei'] = (new OptService())->get_base_info('cai_shougonglei');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_gonghui');
            $data['flowsetname'] = FlowSetModel::get_name('hou_gonghui');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_gonghui');
            $data['isnext'] = (new FlowService())->check_isnext('hou_gonghui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_gonghui(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['shougonglei'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
                if(empty($param['iskou'])){
                    return outputJson(4000, '请选择本次回款是否收取');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            Db::startTrans();
            try {
                HouGonghuiModel::update($param);

                HouGonghuiListModel::where(['gonghuiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gonghuiid'] = $param['id'];
                    $vv['type'] = 1;
                    HouGonghuiListModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_gonghui', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_gonghui', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_gonghui',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_gonghui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_gonghui', $param['id'], '工程回款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouGonghuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $list = HouGonghuiListModel::where(['gonghuiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $piaoinfo = PiaoKaiListModel::alias('list')
                    ->field('list.*,e.name as enginname,kai.shouer,kai.qianpiaotype,kai.dt')
                    ->join('piao_kai kai','kai.id=list.kaiid')
                    ->join('engin e','e.id=kai.enginid')
                    ->where(['list.id'=>$item['piaoid']])->find();

                $item['nums'] = $piaoinfo['nums'];
                $item['shouer'] = $piaoinfo['shouer'];
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $piaoinfo['qianpiaotype']);
                $item['money'] = $piaoinfo['money'];
                $item['yihuikuan'] = HouGonghuiListModel::alias('list')
                    ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                    ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                    ->where(['list.piaoid'=>$item['piaoid']])
                    ->sum('list.xiaofee');
                $item['daihuikuan'] = round($item['money'] - $item['yihuikuan'], 2);
            }
            $data['list'] = $list;

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shougonglei'] = (new OptService())->get_base_info('cai_shougonglei');

            $data['files'] = (new OptService())->get_file('hou_gonghui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_gonghui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_gonghui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_gonghui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_hou_gonghui222(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['shougonglei'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
                if(empty($param['iskou'])){
                    return outputJson(4000, '请选择本次回款是否收取');
                }else{
                    if($param['iskou'] == '是'){
                        if(!$param['koulei']){
                            return outputJson(4000, '选择本次收取款项类型');
                        }
                    }
                }
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'houkoulei') !== false){
                    unset($param[$k]);
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            $kouleiArr = explode(',',$param['koulei']);

            $fuwu_list = array_filter($param['fuwu_list']);
            unset($param['fuwu_list']);
            if($param['isturn'] > 0){
                if(in_array('fuwu',$kouleiArr)){
                    if(count($fuwu_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取劳务分包服务费');
                    }
                }
            }

            $shuifei_list = array_filter($param['shuifei_list']);
            unset($param['shuifei_list']);
            if($param['isturn'] > 0){
                if(in_array('shuifei',$kouleiArr)){
                    if(count($shuifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取税款(含利息)');
                    }
                }
            }



            $gongjie_list = array_filter($param['gongjie_list']);
            unset($param['gongjie_list']);
            if($param['isturn'] > 0){
                if(in_array('gongjie',$kouleiArr)){
                    if(count($gongjie_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取工程负责人借款(含利息)');
                    }
                }
            }

            $kuaifei_list = array_filter($param['kuaifei_list']);
            unset($param['kuaifei_list']);
            if($param['isturn'] > 0){
                if(in_array('kuaifei',$kouleiArr)){
                    if(count($kuaifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取快递费');
                    }
                }
            }

            $qifei_list = array_filter($param['qifei_list']);
            unset($param['qifei_list']);
            if($param['isturn'] > 0){
                if(in_array('qifei',$kouleiArr)){
                    if(count($qifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-直接收取');
                    }
                }
            }

            if($param['iskou'] != '是'){
                $param['koulei'] = '';
                $param['koufee'] = 0;
                $fuwu_list = [];
                $shuifei_list = [];
                $gongjie_list = [];
                $kuaifei_list = [];
                $qifei_list = [];
            }

            Db::startTrans();
            try {
                $id = HouGonghuiModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['gonghuiid'] = $id;
                    HouGonghuiListModel::insert($vv);
                }

                foreach($fuwu_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $id;
                    HouKouFuwuModel::insert($vv);
                }

                foreach($shuifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $id;
                    HouKouShuifeiModel::insert($vv);
                }

                foreach($gongjie_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $id;
                    HouKouGongjieModel::insert($vv);
                }

                foreach($kuaifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $id;
                    if($vv['kuaitype'] == 1){
                        $vv['enginid'] = KuaiJiModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }else{
                        $vv['enginid'] = KuaiShouModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }
                    HouKouKuaifeiModel::insert($vv);
                }

                foreach($qifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $id;
                    HouKouQifeiModel::insert($vv);
                }

                (new FlowService())->save_bill('hou_gonghui', $id);

                (new OptService())->relation_file($files,'hou_gonghui',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_gonghui', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_gonghui', $id, '工程回款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $enginid = $request->get('enginid',0,'int');
            $data['engininfo'] = (new EnginService())->get_engin_showinfo($enginid);

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shougonglei'] = (new OptService())->get_base_info('cai_shougonglei');
            $data['houkoulei'] = (new OptService())->get_base_info('cai_houkoulei', 2);

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_gonghui');
            $data['flowsetname'] = FlowSetModel::get_name('hou_gonghui');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_gonghui');
            $data['isnext'] = (new FlowService())->check_isnext('hou_gonghui');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_gonghui222(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['enginid'])){
                    return outputJson(4000, '请选择工程名称');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['shougonglei'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
                if(empty($param['iskou'])){
                    return outputJson(4000, '请选择本次回款是否收取');
                }else{
                    if($param['iskou'] == '是'){
                        if(!$param['koulei']){
                            return outputJson(4000, '选择本次收取款项类型');
                        }
                    }
                }
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'houkoulei') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);


            $kouleiArr = explode(',',$param['koulei']);

            $fuwu_list = array_filter($param['fuwu_list']);
            unset($param['fuwu_list']);
            if($param['isturn'] > 0){
                if(in_array('fuwu',$kouleiArr)){
                    if(count($fuwu_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取劳务分包服务费');
                    }
                }
            }

            $shuifei_list = array_filter($param['shuifei_list']);
            unset($param['shuifei_list']);
            if($param['isturn'] > 0){
                if(in_array('shuifei',$kouleiArr)){
                    if(count($shuifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取税款(含利息)');
                    }
                }
            }



            $gongjie_list = array_filter($param['gongjie_list']);
            unset($param['gongjie_list']);
            if($param['isturn'] > 0){
                if(in_array('gongjie',$kouleiArr)){
                    if(count($gongjie_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取工程负责人借款(含利息)');
                    }
                }
            }

            $kuaifei_list = array_filter($param['kuaifei_list']);
            unset($param['kuaifei_list']);
            if($param['isturn'] > 0){
                if(in_array('kuaifei',$kouleiArr)){
                    if(count($kuaifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取快递费');
                    }
                }
            }

            $qifei_list = array_filter($param['qifei_list']);
            unset($param['qifei_list']);
            if($param['isturn'] > 0){
                if(in_array('qifei',$kouleiArr)){
                    if(count($qifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-直接收取');
                    }
                }
            }

            if($param['iskou'] != '是'){
                $param['koulei'] = '';
                $param['koufee'] = 0;
                $fuwu_list = [];
                $shuifei_list = [];
                $gongjie_list = [];
                $kuaifei_list = [];
                $qifei_list = [];
            }


            Db::startTrans();
            try {
                HouGonghuiModel::update($param);

                HouGonghuiListModel::where(['gonghuiid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['gonghuiid'] = $param['id'];
                    HouGonghuiListModel::insert($vv);
                }

                HouKouFuwuModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($fuwu_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouFuwuModel::insert($vv);
                }

                HouKouShuifeiModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($shuifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouShuifeiModel::insert($vv);
                }

                HouKouGongjieModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($gongjie_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouGongjieModel::insert($vv);
                }

                HouKouKuaifeiModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($kuaifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    if($vv['kuaitype'] == 1){
                        $vv['enginid'] = KuaiJiModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }else{
                        $vv['enginid'] = KuaiShouModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }
                    HouKouKuaifeiModel::insert($vv);
                }

                HouKouQifeiModel::where(['zhuid'=>$param['id'],'type'=>1])->delete();
                foreach($qifei_list as $vv){
                    $vv['type'] = 1;
                    $vv['zhuid'] = $param['id'];
                    HouKouQifeiModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_gonghui', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_gonghui', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_gonghui',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_gonghui', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_gonghui', $param['id'], '工程回款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouGonghuiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $list = HouGonghuiListModel::where(['gonghuiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $piaoinfo = PiaoKaiListModel::alias('list')
                    ->field('list.*,e.name as enginname,kai.shouer,kai.qianpiaotype,kai.dt')
                    ->join('piao_kai kai','kai.id=list.kaiid')
                    ->join('engin e','e.id=kai.enginid')
                    ->where(['list.id'=>$item['piaoid']])->find();

                $item['nums'] = $piaoinfo['nums'];
                $item['shouer'] = $piaoinfo['shouer'];
                $item['qianpiaotype_name'] = (new OptService())->get_base_showname('cai_qianpiaotype', $piaoinfo['qianpiaotype']);
                $item['money'] = $piaoinfo['money'];
                $item['yihuikuan'] = HouGonghuiListModel::alias('list')
                    ->join('hou_gonghui hui','hui.id=list.gonghuiid')
                    ->where(['hui.comid'=>$this->userinfo['comid'],'hui.deleted_user'=>0,'hui.isturn'=>1,'hui.status'=>1])
                    ->where(['list.piaoid'=>$item['piaoid']])
                    ->sum('list.xiaofee');
                $item['daihuikuan'] = round($item['money'] - $item['yihuikuan'], 2);
            }
            $data['list'] = $list;


            if($data['data']['iskou'] == '是'){
                $data['data']['kouleiArr'] = explode(',', $data['data']['koulei']);

                $fuwu_list = HouKouFuwuModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($fuwu_list as &$vvfuwu){
                    $vvfuwu['name'] = EnginModel::where(['id'=>$vvfuwu['enginid']])->value('name');

                    $vvfuwu['shimoney'] = (new EnginService())->get_contract_shi_money($vvfuwu['enginid']);
                    $vvfuwu['shijiemoney'] = (new EnginService())->get_contract_shi_jie_money($vvfuwu['enginid']);

                    $vvfuwu['ratefu'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['enginid'=>$vvfuwu['enginid']])->order('id DESC')->value('ratefu');

                    $vvfuwu['yingkoufee'] = 0;
                    if($vvfuwu['shijiemoney']){
                        $vvfuwu['yingkoufee'] = round($vvfuwu['shijiemoney']*$vvfuwu['ratefu']/100, 2);
                    }else{
                        $vvfuwu['yingkoufee'] = round($vvfuwu['shimoney']*$vvfuwu['ratefu']/100, 2);
                    }

                    $vvfuwu['yikoufee'] = HouKouFuwuModel::alias('list')
                        ->join('hou_gonghui hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>1,'list.enginid'=>$vvfuwu['enginid']])
                        ->sum('list.koufee');
                    $vvfuwu['yikoufee'] += HouKouFuwuModel::alias('list')
                        ->join('hou_gongyu hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>2,'list.enginid'=>$vvfuwu['enginid']])
                        ->sum('list.koufee');

                    $vvfuwu['daikoufee'] = round($vvfuwu['yingkoufee'] - $vvfuwu['yikoufee'], 2);
                    $vvfuwu['daikoufee'] = $vvfuwu['daikoufee']  > 0 ? $vvfuwu['daikoufee'] : 0;
                }
                $data['fuwu_list'] = $fuwu_list;


                $shuifei_list = HouKouShuifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($shuifei_list as &$vvshuifei){
                    $shuiinfo = EnginShuiModel::where(['id'=>$vvshuifei['shuiid']])->find();
                    $piaoinfo = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['enginid'=>$shuiinfo['enginid'],'shuiid'=>$vvshuifei['shuiid']])->find();

                    $vvshuifei['name'] = $shuiinfo['name'];
                    $vvshuifei['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
                    $vvshuifei['shendt'] = date("Y-m-d", strtotime($shuiinfo['created_at']));
                    $vvshuifei['xiaofee'] = $shuiinfo['xiaofee'];
                    $vvshuifei['dt'] = $piaoinfo['dt'];
                    $vvshuifei['baofee'] = $shuiinfo['baofee'];
                }
                $data['shuifei_list'] = $shuifei_list;


                $gongjie_list = HouKouGongjieModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($gongjie_list as &$vvgongjie){
                    $jieinfo = HouJieModel::where(['id'=>$vvgongjie['jieid']])->find();
                    $vvgongjie['number'] = $jieinfo['number'];
                    $vvgongjie['chuer'] = $jieinfo['chuer'];
                    $vvgongjie['money'] = $jieinfo['money'];
                    $vvgongjie['startdt'] = $jieinfo['startdt'];
                    $vvgongjie['enddt'] = $jieinfo['enddt'];
                    $vvgongjie['yueshu'] = 0;
                    $vvgongjie['lixifee'] = 0;
                    $vvgongjie['fangkuandt'] = '';
                    //月数  （还款日期 - 放款日期 + 1）/ 30
                    //利息  月数   * 月利息 / 100 * 借款金额
                    $houshou = HouShouModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['jieid'=>$vvgongjie['jieid']])->order('daodt ASC')->find();
                    if($houshou){
                        $vvgongjie['fangkuandt'] = $houshou['daodt'];
                        $vvgongjie['yueshu'] = ceil(  (( strtotime($data['data']['dt']) - strtotime($vvgongjie['fangkuandt']))/86400 + 1)/30);
                        $vvgongjie['yueshu'] = $vvgongjie['yueshu'] > 0 ? $vvgongjie['yueshu'] : 0;
                        $vvgongjie['lixifee'] = round($vvgongjie['yueshu'] * $jieinfo['yuerate'] / 100 * $vvgongjie['money'], 2);
                    }
                    $vvgongjie['yinghuanfee'] = round($vvgongjie['money']+$vvgongjie['lixifee'],2);
                }
                $data['gongjie_list'] = $gongjie_list;


                $kuaifei_list = HouKouKuaifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($kuaifei_list as &$vvkuaifei){
                    if($vvkuaifei['kuaitype'] == 1){
                        $jiinfo = KuaiJiModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                        $vvkuaifei['enginname'] = EnginModel::where(['id'=>$jiinfo['enginid']])->value('name');
                        $vvkuaifei['kuailei'] = '寄件';
                        $vvkuaifei['yunnum'] = $jiinfo['yunnum'];
                        $vvkuaifei['kuaidt'] = $jiinfo['jidt'];
                        $vvkuaifei['futype'] = $jiinfo['futype'];
                        $vvkuaifei['kuaifee'] = $jiinfo['kuaifee'];
                    }else{
                        $shouinfo = KuaiShouModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                        $vvkuaifei['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
                        $vvkuaifei['kuailei'] = '收件';
                        $vvkuaifei['yunnum'] = $shouinfo['yunnum'];
                        $vvkuaifei['kuaidt'] = $shouinfo['jidt'];
                        $vvkuaifei['futype'] = $shouinfo['futype'];
                        $vvkuaifei['kuaifee'] = $shouinfo['kuaifee'];
                    }
                }
                $data['kuaifei_list'] = $kuaifei_list;


                $qifei_list = HouKouQifeiModel::where(['type'=>1,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($qifei_list as &$vvqifei){
                    $vvqifei['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
                }
                $data['qifei_list'] = $qifei_list;
            }




            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shougonglei'] = (new OptService())->get_base_info('cai_shougonglei');
            $data['houkoulei'] = (new OptService())->get_base_info('cai_houkoulei', 2);

            $data['files'] = (new OptService())->get_file('hou_gonghui', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_gonghui');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_gonghui',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_gonghui');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_hou_gongyu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['gofuer'])){
                    return outputJson(4000, '请选择工程负责人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
                if(empty($param['iskou'])){
                    return outputJson(4000, '请选择本次回款是否收取');
                }else{
                    if($param['iskou'] == '是'){
                        if(!$param['koulei']){
                            return outputJson(4000, '选择本次收取款项类型');
                        }
                    }
                }
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'houkoulei') !== false){
                    unset($param[$k]);
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            $kouleiArr = explode(',',$param['koulei']);

            $fuwu_list = array_filter($param['fuwu_list']);
            unset($param['fuwu_list']);
            if($param['isturn'] > 0){
                if(in_array('fuwu',$kouleiArr)){
                    if(count($fuwu_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取劳务分包服务费');
                    }
                }else{
                    $fuwu_list = [];
                }
            }

            $shuifei_list = array_filter($param['shuifei_list']);
            unset($param['shuifei_list']);
            if($param['isturn'] > 0){
                if(in_array('shuifei',$kouleiArr)){
                    if(count($shuifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取税款(含利息)');
                    }
                }else{
                    $shuifei_list = [];
                }
            }



            $gongjie_list = array_filter($param['gongjie_list']);
            unset($param['gongjie_list']);
            if($param['isturn'] > 0){
                if(in_array('gongjie',$kouleiArr)){
                    if(count($gongjie_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取工程负责人借款(含利息)');
                    }
                }else{
                    $gongjie_list = [];
                }
            }

            $kuaifei_list = array_filter($param['kuaifei_list']);
            unset($param['kuaifei_list']);
            if($param['isturn'] > 0){
                if(in_array('kuaifei',$kouleiArr)){
                    if(count($kuaifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取快递费');
                    }
                }else{
                    $kuaifei_list = [];
                }
            }

            $qifei_list = array_filter($param['qifei_list']);
            unset($param['qifei_list']);
            if($param['isturn'] > 0){
                if(in_array('qifei',$kouleiArr)){
                    if(count($qifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-直接收取');
                    }
                }else{
                    $qifei_list = [];
                }
            }

            if($param['iskou'] != '是'){
                $param['koulei'] = '';
                $param['koufee'] = 0;
                $fuwu_list = [];
                $shuifei_list = [];
                $gongjie_list = [];
                $kuaifei_list = [];
                $qifei_list = [];
            }

            Db::startTrans();
            try {
                $id = HouGongyuModel::insertGetId($param);

                foreach($fuwu_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $id;
                    HouKouFuwuModel::insert($vv);
                }

                foreach($shuifei_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $id;
                    HouKouShuifeiModel::insert($vv);
                }

                foreach($gongjie_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $id;
                    HouKouGongjieModel::insert($vv);
                }

                foreach($kuaifei_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $id;
                    if($vv['kuaitype'] == 1){
                        $vv['enginid'] = KuaiJiModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }else{
                        $vv['enginid'] = KuaiShouModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }
                    HouKouKuaifeiModel::insert($vv);
                }

                foreach($qifei_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $id;
                    HouKouQifeiModel::insert($vv);
                }

                (new FlowService())->save_bill('hou_gongyu', $id);

                (new OptService())->relation_file($files,'hou_gongyu',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_gongyu', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_gongyu', $id, '工程负责人预收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shougonglei'] = (new OptService())->get_base_info('cai_shougonglei');

            $houkoulei = (new OptService())->get_base_info('cai_houkoulei');
            foreach ($houkoulei as $kkk=>$fff){
                if(in_array($fff['name'], ['劳务分包服务费','税费利息','快递费','借款'])){
                    unset($houkoulei[$kkk]);
                }
            }
            sort($houkoulei);
            $data['houkoulei'] = $houkoulei;

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_gongyu');
            $data['flowsetname'] = FlowSetModel::get_name('hou_gongyu');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_gongyu');
            $data['isnext'] = (new FlowService())->check_isnext('hou_gongyu');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_gongyu(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['gofuer'])){
                    return outputJson(4000, '请选择工程负责人');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
                if(empty($param['iskou'])){
                    return outputJson(4000, '请选择本次回款是否收取');
                }else{
                    if($param['iskou'] == '是'){
                        if(!$param['koulei']){
                            return outputJson(4000, '选择本次收取款项类型');
                        }
                    }
                }
            }


            foreach ($param as $k=>$v){
                if(strpos($k,'houkoulei') !== false){
                    unset($param[$k]);
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);


            $kouleiArr = explode(',',$param['koulei']);

            $fuwu_list = array_filter($param['fuwu_list']);
            unset($param['fuwu_list']);
            if($param['isturn'] > 0){
                if(in_array('fuwu',$kouleiArr)){
                    if(count($fuwu_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取劳务分包服务费');
                    }
                }else{
                    $fuwu_list = [];
                }
            }

            $shuifei_list = array_filter($param['shuifei_list']);
            unset($param['shuifei_list']);
            if($param['isturn'] > 0){
                if(in_array('shuifei',$kouleiArr)){
                    if(count($shuifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取税款(含利息)');
                    }
                }else{
                    $shuifei_list = [];
                }
            }



            $gongjie_list = array_filter($param['gongjie_list']);
            unset($param['gongjie_list']);
            if($param['isturn'] > 0){
                if(in_array('gongjie',$kouleiArr)){
                    if(count($gongjie_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取工程负责人借款(含利息)');
                    }
                }else{
                    $gongjie_list = [];
                }
            }

            $kuaifei_list = array_filter($param['kuaifei_list']);
            unset($param['kuaifei_list']);
            if($param['isturn'] > 0){
                if(in_array('kuaifei',$kouleiArr)){
                    if(count($kuaifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-待收取快递费');
                    }
                }else{
                    $kuaifei_list = [];
                }
            }

            $qifei_list = array_filter($param['qifei_list']);
            unset($param['qifei_list']);
            if($param['isturn'] > 0){
                if(in_array('qifei',$kouleiArr)){
                    if(count($qifei_list) < 1){
                        return outputJson(4000, '请输入收取明细-直接收取');
                    }
                }else{
                    $qifei_list = [];
                }
            }

            if($param['iskou'] != '是'){
                $param['koulei'] = '';
                $param['koufee'] = 0;
                $fuwu_list = [];
                $shuifei_list = [];
                $gongjie_list = [];
                $kuaifei_list = [];
                $qifei_list = [];
            }


            Db::startTrans();
            try {
                HouGongyuModel::update($param);


                HouKouFuwuModel::where(['zhuid'=>$param['id'],'type'=>2])->delete();
                foreach($fuwu_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $param['id'];
                    HouKouFuwuModel::insert($vv);
                }

                HouKouShuifeiModel::where(['zhuid'=>$param['id'],'type'=>2])->delete();
                foreach($shuifei_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $param['id'];
                    HouKouShuifeiModel::insert($vv);
                }

                HouKouGongjieModel::where(['zhuid'=>$param['id'],'type'=>2])->delete();
                foreach($gongjie_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $param['id'];
                    HouKouGongjieModel::insert($vv);
                }

                HouKouKuaifeiModel::where(['zhuid'=>$param['id'],'type'=>2])->delete();
                foreach($kuaifei_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $param['id'];
                    if($vv['kuaitype'] == 1){
                        $vv['enginid'] = KuaiJiModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }else{
                        $vv['enginid'] = KuaiShouModel::where(['id'=>$vv['kuaiid']])->value('enginid');
                    }
                    HouKouKuaifeiModel::insert($vv);
                }

                HouKouQifeiModel::where(['zhuid'=>$param['id'],'type'=>2])->delete();
                foreach($qifei_list as $vv){
                    $vv['type'] = 2;
                    $vv['zhuid'] = $param['id'];
                    HouKouQifeiModel::insert($vv);
                }

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_gongyu', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_gongyu', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_gongyu',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_gongyu', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_gongyu', $param['id'], '工程负责人预收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouGongyuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['tonger_info'] = (new UserService())->get_users_part_arr($data['data']['tonger'], 'user');
            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            if($data['data']['gofuer']){
                $data['data']['hougongyumoney'] = (new HouService())->get_hou_gongyu_money($data['data']['gofuer']);
                $data['data']['hougongzhimoneyqugoer'] = (new HouService())->get_hou_gongzhi_money_qugoer($data['data']['gofuer']);
                $data['data']['hougongyukoufee'] = (new HouService())->get_hou_gongyu_koufee($data['data']['gofuer']);
                $data['data']['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($data['data']['gofuer']);
                $data['data']['qizankoufee'] = HouKouQifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2, 'hou.gofuer'=>$data['data']['gofuer'],'kouxing'=>'暂扣'])
                    ->sum('list.koufee');
                $data['data']['jiedongkoufee'] = HouKouQifeiModel::alias('list')
                    ->join('hou_gongyu hou','hou.id=list.zhuid')
                    ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                    ->where(['list.type'=>2, 'hou.gofuer'=>$data['data']['gofuer'],'isdong'=>1])
                    ->sum('list.koufee');

                $data['data']['yuliufee'] = round($data['data']['qizankoufee'] - $data['data']['jiedongkoufee'], 2);
                $data['data']['hougongshuimoneygoquan'] = (new HouService())->get_hou_gongshui_money_goquan($data['data']['gofuer']);
                $data['data']['hougongyukeyong'] = round($data['data']['hougongyumoney']-$data['data']['hougongyukoufee']-$data['data']['hougongzhimoneyqugoer']+$data['data']['jiedongkoufee']+$data['data']['hougongshuimoneygoquan'],2);


            }





            if($data['data']['iskou'] == '是'){
                $data['data']['kouleiArr'] = explode(',', $data['data']['koulei']);

                $fuwu_list = HouKouFuwuModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($fuwu_list as &$vvfuwu){
                    $vvfuwu['name'] = EnginModel::where(['id'=>$vvfuwu['enginid']])->value('name');

                    $vvfuwu['shimoney'] = (new EnginService())->get_contract_shi_money($vvfuwu['enginid']);
                    $vvfuwu['shijiemoney'] = (new EnginService())->get_contract_shi_jie_money($vvfuwu['enginid']);

                    $vvfuwu['ratefu'] = ContractNeiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['enginid'=>$vvfuwu['enginid']])->order('id DESC')->value('ratefu');

                    $vvfuwu['yingkoufee'] = 0;
                    if($vvfuwu['shijiemoney']){
                        $vvfuwu['yingkoufee'] = round($vvfuwu['shijiemoney']*$vvfuwu['ratefu']/100, 2);
                    }else{
                        $vvfuwu['yingkoufee'] = round($vvfuwu['shimoney']*$vvfuwu['ratefu']/100, 2);
                    }

                    $vvfuwu['yikoufee'] = HouKouFuwuModel::alias('list')
                        ->join('hou_gonghui hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>1,'list.enginid'=>$vvfuwu['enginid']])
                        ->sum('list.koufee');
                    $vvfuwu['yikoufee'] += HouKouFuwuModel::alias('list')
                        ->join('hou_gongyu hou','hou.id=list.zhuid')
                        ->where(['hou.comid'=>$this->userinfo['comid'],'hou.deleted_user'=>0,'hou.isturn'=>1,'hou.status'=>1])
                        ->where(['list.type'=>2,'list.enginid'=>$vvfuwu['enginid']])
                        ->sum('list.koufee');

                    $vvfuwu['daikoufee'] = round($vvfuwu['yingkoufee'] - $vvfuwu['yikoufee'], 2);
                    $vvfuwu['daikoufee'] = $vvfuwu['daikoufee']  > 0 ? $vvfuwu['daikoufee'] : 0;
                }
                $data['fuwu_list'] = $fuwu_list;


                $shuifei_list = HouKouShuifeiModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($shuifei_list as &$vvshuifei){
                    $shuiinfo = EnginShuiModel::where(['id'=>$vvshuifei['shuiid']])->find();
                    $piaoinfo = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                        ->where(['enginid'=>$shuiinfo['enginid'],'shuiid'=>$vvshuifei['shuiid']])->find();

                    $vvshuifei['name'] = $shuiinfo['name'];
                    $vvshuifei['enginname'] = EnginModel::where(['id'=>$shuiinfo['enginid']])->value('name');
                    $vvshuifei['shendt'] = date("Y-m-d", strtotime($shuiinfo['created_at']));
                    $vvshuifei['xiaofee'] = $shuiinfo['xiaofee'];
                    $vvshuifei['dt'] = $piaoinfo['dt'];
                    $vvshuifei['baofee'] = $shuiinfo['baofee'];
                }
                $data['shuifei_list'] = $shuifei_list;


                $gongjie_list = HouKouGongjieModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($gongjie_list as &$vvgongjie){
                    $tiaoinfo = HouTiaoModel::where(['id'=>$vvgongjie['tiaoid']])->find();
                    $tiaoinfo = (new HouService())->get_tiaoinfo($tiaoinfo, $data['data']['dt']);
                    $vvgongjie['number'] = $tiaoinfo['number'];
                    $vvgongjie['chuer'] = $tiaoinfo['chuer'];
                    $vvgongjie['money'] = $tiaoinfo['money'];
                    $vvgongjie['daodt'] = $tiaoinfo['daodt'];
                    $vvgongjie['yueshu_zhan'] = $tiaoinfo['yueshu_zhan'];
                    $vvgongjie['daihuanbenfee'] = $tiaoinfo['daihuanbenfee'];
                    $vvgongjie['daihuanlifee'] = $tiaoinfo['daihuanlifee'];
                    $vvgongjie['daihuanallfee'] = $tiaoinfo['daihuanallfee'];
                }
                $data['gongjie_list'] = $gongjie_list;


                $kuaifei_list = HouKouKuaifeiModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($kuaifei_list as &$vvkuaifei){
                    if($vvkuaifei['kuaitype'] == 1){
                        $jiinfo = KuaiJiModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                        $vvkuaifei['enginname'] = EnginModel::where(['id'=>$jiinfo['enginid']])->value('name');
                        $vvkuaifei['kuailei'] = '寄件';
                        $vvkuaifei['yunnum'] = $jiinfo['yunnum'];
                        $vvkuaifei['kuaidt'] = $jiinfo['jidt'];
                        $vvkuaifei['futype'] = $jiinfo['futype'];
                        $vvkuaifei['kuaifee'] = $jiinfo['kuaifee'];
                    }else{
                        $shouinfo = KuaiShouModel::where(['id'=>$vvkuaifei['kuaiid']])->find();
                        $vvkuaifei['enginname'] = EnginModel::where(['id'=>$shouinfo['enginid']])->value('name');
                        $vvkuaifei['kuailei'] = '收件';
                        $vvkuaifei['yunnum'] = $shouinfo['yunnum'];
                        $vvkuaifei['kuaidt'] = $shouinfo['jidt'];
                        $vvkuaifei['futype'] = $shouinfo['futype'];
                        $vvkuaifei['kuaifee'] = $shouinfo['kuaifee'];
                    }
                }
                $data['kuaifei_list'] = $kuaifei_list;


                $qifei_list = HouKouQifeiModel::where(['type'=>2,'zhuid'=>$data['data']['id']])->select()->toArray();
                foreach ($qifei_list as &$vvqifei){
                    $vvqifei['enginname'] = EnginModel::where(['id'=>$vvqifei['enginid']])->value('name');
                }
                $data['qifei_list'] = $qifei_list;
            }




            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shougonglei'] = (new OptService())->get_base_info('cai_shougonglei');

            $houkoulei = (new OptService())->get_base_info('cai_houkoulei');
            foreach ($houkoulei as $kkk=>$fff){
                if(in_array($fff['name'], ['劳务分包服务费','税费利息','快递费','借款'])){
                    unset($houkoulei[$kkk]);
                }
            }
            sort($houkoulei);
            $data['houkoulei'] = $houkoulei;

            $data['files'] = (new OptService())->get_file('hou_gongyu', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_gongyu');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_gongyu',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_gongyu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_hou_dayshou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['shouqilei'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请输入收款对象');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = HouDayshouModel::insertGetId($param);

                (new FlowService())->save_bill('hou_dayshou', $id);

                (new OptService())->relation_file($files,'hou_dayshou',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_dayshou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_dayshou', $id, '日常收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shouqilei'] = (new OptService())->get_base_info('cai_shouqilei');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_dayshou');
            $data['flowsetname'] = FlowSetModel::get_name('hou_dayshou');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_dayshou');
            $data['isnext'] = (new FlowService())->check_isnext('hou_dayshou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_dayshou(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if($param['isturn'] > 0){
                if(empty($param['shouqilei'])){
                    return outputJson(4000, '请选择款项类型');
                }
                if(empty($param['zhai'])){
                    return outputJson(4000, '请输入款项摘要');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请输入收款对象');
                }
                if(empty($param['incdaotype'])){
                    return outputJson(4000, '请选择到账方式');
                }
                if(empty($param['accid'])){
                    return outputJson(4000, '请选择到账账户');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择到账日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                HouDayshouModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_dayshou', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_dayshou', $param['id']);
                }

                (new OptService())->relation_file($files,'hou_dayshou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_dayshou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_dayshou', $param['id'], '日常收款');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouDayshouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['account'] = AccountModel::get_info_by_pk($data['data']['accid']);

            $data['incdaotype'] = (new OptService())->get_base_info('cai_income_incdaotype');
            $data['shouqilei'] = (new OptService())->get_base_info('cai_shouqilei');


            $data['files'] = (new OptService())->get_file('hou_dayshou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_dayshou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_dayshou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_dayshou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }



    public function add_hou_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['chuer'])){
                    return outputJson(4000, '请选择出借方');
                }
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借款人');
                }
                if(empty($param['jie_tel'])){
                    return outputJson(4000, '请输入借款人联系电话');
                }
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['fangtype'])){
                    return outputJson(4000, '请选择放款方式');
                }
                if(empty($param['qiandt'])){
                    return outputJson(4000, '请选择签订日期');
                }

                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }

                if(!empty($param['daner'])){
                    if(empty($param['dan_tel'])){
                        return outputJson(4000, '请输入担保人1联系电话');
                    }
                }
                if(!empty($param['twoer'])){
                    if(empty($param['two_tel'])){
                        return outputJson(4000, '请输入担保人2联系电话');
                    }
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);



            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入收款方信息');
                }

                $listMoney = 0;
                foreach ($list as $mmm){
                    $listMoney += $mmm['money'];
                }
                if(round($listMoney, 2) != round($param['money'], 2)){
                    return outputJson(4000, '打款金额与借款金额不等，请修改后提交。');
                }
            }





            Db::startTrans();
            try {
                $id = HouJieModel::insertGetId($param);

                foreach($list as $vv){
                    $vv['jieid'] = $id;
                    HouJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'hou_jie',$id);
                (new FlowService())->save_bill('hou_jie', $id);


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_jie', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_jie', $id, '借款登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['fangtype'] = (new OptService())->get_base_info('cai_jie_fangtype');
            $data['qiantype'] = (new ContractService())->get_suo_yewu();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_jie');
            $data['flowsetname'] = FlowSetModel::get_name('hou_jie');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_jie');
            $data['isnext'] = (new FlowService())->check_isnext('hou_jie');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_jie(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['chuer'])){
                    return outputJson(4000, '请选择出借方');
                }
                if(empty($param['jieer'])){
                    return outputJson(4000, '请选择借款人');
                }
                if(empty($param['jie_tel'])){
                    return outputJson(4000, '请输入借款人联系电话');
                }
                if(empty($param['yong'])){
                    return outputJson(4000, '请输入用途');
                }
                if(empty($param['fangtype'])){
                    return outputJson(4000, '请选择放款方式');
                }
                if(empty($param['qiandt'])){
                    return outputJson(4000, '请选择签订日期');
                }

                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }

                if(!empty($param['daner'])){
                    if(empty($param['dan_tel'])){
                        return outputJson(4000, '请输入担保人1联系电话');
                    }
                }
                if(!empty($param['twoer'])){
                    if(empty($param['two_tel'])){
                        return outputJson(4000, '请输入担保人2联系电话');
                    }
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);
            if($param['isturn'] > 0){
                if(count($list) < 1){
                    return outputJson(4000, '请输入收款方信息');
                }

                $listMoney = 0;
                foreach ($list as $mmm){
                    $listMoney += $mmm['money'];
                }
                if(round($listMoney, 2) != round($param['money'], 2)){
                    return outputJson(4000, '打款金额与借款金额不等，请修改后提交。');
                }
            }

            Db::startTrans();
            try {
                HouJieModel::update($param);

                HouJieListModel::where(['jieid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['jieid'] = $param['id'];
                    HouJieListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'hou_jie',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_jie', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_jie', $param['id']);
                }


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_jie', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_jie', $param['id'], '借款登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouJieModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['fangtype'] = (new OptService())->get_base_info('cai_jie_fangtype');
            $data['qiantype'] = (new ContractService())->get_suo_yewu();


            $list = HouJieListModel::where(['jieid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('hou_jie', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_jie');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_jie',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_jie');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_hou_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieid'])){
                    return outputJson(4000, '请选择借款信息');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = HouShouModel::insertGetId($param);
                (new OptService())->relation_file($files,'hou_shou',$id);
                (new FlowService())->save_bill('hou_shou', $id);


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_shou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_shou', $id, '借款收条');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_shou');
            $data['flowsetname'] = FlowSetModel::get_name('hou_shou');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_shou');
            $data['isnext'] = (new FlowService())->check_isnext('hou_shou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieid'])){
                    return outputJson(4000, '请选择借款信息');
                }
                if($param['state'] == ''){
                    return outputJson(4000, '请选择签章状态');
                }
                if(empty($param['qiantype'])){
                    return outputJson(4000, '请选择签章类型');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                HouShouModel::update($param);
                (new OptService())->relation_file($files,'hou_shou',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_shou', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_shou', $param['id']);
                }


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_shou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_shou', $param['id'], '借款收条');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $jieinfo = HouJieModel::where(['id'=>$data['data']['jieid']])->find();
            $data['jieinfo'] = $jieinfo;
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['qiantype'] = (new ContractService())->get_suo_yewu();
            $data['files'] = (new OptService())->get_file('hou_shou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_shou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_shou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_shou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_hou_tiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieid'])){
                    return outputJson(4000, '请选择借款信息');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次放款金额');
                }
                if(empty($param['daodt'])){
                    return outputJson(4000, '请选择放款到账日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = HouTiaoModel::insertGetId($param);
                (new OptService())->relation_file($files,'hou_tiao',$id);
                (new FlowService())->save_bill('hou_tiao', $id);

                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'hou_tiao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'hou_tiao', $id, '借款收条');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('hou_tiao');
            $data['flowsetname'] = FlowSetModel::get_name('hou_tiao');
            $data['isadvan'] = (new FlowService())->check_isadvan('hou_tiao');
            $data['isnext'] = (new FlowService())->check_isnext('hou_tiao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_hou_tiao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jieid'])){
                    return outputJson(4000, '请选择借款信息');
                }
                if($param['money'] == ''){
                    return outputJson(4000, '请输入本次放款金额');
                }
                if(empty($param['daodt'])){
                    return outputJson(4000, '请选择放款到账日期');
                }
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                HouTiaoModel::update($param);
                (new OptService())->relation_file($files,'hou_tiao',$param['id']);
                if($isshen > 0){
                    (new FlowService())->save_bill_shen('hou_tiao', $param['id']);
                }else{
                    (new FlowService())->save_bill('hou_tiao', $param['id']);
                }


                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'hou_tiao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'hou_tiao', $param['id'], '借款收条');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = HouTiaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $jieinfo = HouJieModel::where(['id'=>$data['data']['jieid']])->find();
            if($jieinfo){
                //放款合计金额
                $jieinfo['fangfee'] = HouTiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'isturn'=>1])
                    ->where(['jieid'=>$jieinfo['id']])->sum('money');
                $jieinfo['daifee'] = round($jieinfo['money'] - $jieinfo['fangfee'], 2);
            }
            $data['jieinfo'] = $jieinfo;

            $data['files'] = (new OptService())->get_file('hou_tiao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('hou_tiao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('hou_tiao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('hou_tiao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_kuai_ji(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jier'])){
                    return outputJson(4000, '请选择寄件人');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请输入收件人');
                }
                if(empty($param['shoutel'])){
                    return outputJson(4000, '请输入收件人电话');
                }
                if(empty($param['shouaddr'])){
                    return outputJson(4000, '请输入收件地址');
                }
                if(empty($param['futype'])){
                    return outputJson(4000, '请选择付款方式');
                }
                if(empty($param['isgua'])){
                    return outputJson(4000, '请选择是否工程挂账');
                }
                if($param['isgua'] == '是'){
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择工程名称');
                    }
                }
                if(empty($param['nei'])){
                    return outputJson(4000, '请输入快件内容');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = KuaiJiModel::insertGetId($param);

                (new FlowService())->save_bill('kuai_ji', $id);

                (new OptService())->relation_file($files,'kuai_ji',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kuai_ji', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kuai_ji', $id, '快递寄件');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['jier_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('kuai_ji');
            $data['flowsetname'] = FlowSetModel::get_name('kuai_ji');
            $data['isadvan'] = (new FlowService())->check_isadvan('kuai_ji');
            $data['isnext'] = (new FlowService())->check_isnext('kuai_ji');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kuai_ji(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jier'])){
                    return outputJson(4000, '请选择寄件人');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请输入收件人');
                }
                if(empty($param['shoutel'])){
                    return outputJson(4000, '请输入收件人电话');
                }
                if(empty($param['shouaddr'])){
                    return outputJson(4000, '请输入收件地址');
                }
                if(empty($param['futype'])){
                    return outputJson(4000, '请选择付款方式');
                }
                if(empty($param['isgua'])){
                    return outputJson(4000, '请选择是否工程挂账');
                }
                if($param['isgua'] == '是'){
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择工程名称');
                    }
                }
                if(empty($param['nei'])){
                    return outputJson(4000, '请输入快件内容');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                KuaiJiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kuai_ji', $param['id']);
                }else{
                    (new FlowService())->save_bill('kuai_ji', $param['id']);
                }

                (new OptService())->relation_file($files,'kuai_ji',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kuai_ji', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kuai_ji', $param['id'], '快递寄件');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KuaiJiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['jier_info'] = (new UserService())->get_users_part_arr($data['data']['jier'], 'user');


            $data['files'] = (new OptService())->get_file('kuai_ji', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kuai_ji');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kuai_ji',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kuai_ji');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_kuai_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jier'])){
                    return outputJson(4000, '请选择收件人');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请输入寄件人');
                }
                if(empty($param['futype'])){
                    return outputJson(4000, '请选择付款方式');
                }
                if(empty($param['isgua'])){
                    return outputJson(4000, '请选择是否工程挂账');
                }
                if($param['isgua'] == '是'){
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择工程名称');
                    }
                }
                if(empty($param['jidt'])){
                    return outputJson(4000, '请选择收件日期');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = KuaiShouModel::insertGetId($param);

                (new FlowService())->save_bill('kuai_shou', $id);

                (new OptService())->relation_file($files,'kuai_shou',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'kuai_shou', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'kuai_shou', $id, '快递收件');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('kuai_shou');
            $data['flowsetname'] = FlowSetModel::get_name('kuai_shou');
            $data['isadvan'] = (new FlowService())->check_isadvan('kuai_shou');
            $data['isnext'] = (new FlowService())->check_isnext('kuai_shou');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_kuai_shou(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['jier'])){
                    return outputJson(4000, '请选择收件人');
                }
                if(empty($param['shouer'])){
                    return outputJson(4000, '请输入寄件人');
                }
                if(empty($param['futype'])){
                    return outputJson(4000, '请选择付款方式');
                }
                if(empty($param['isgua'])){
                    return outputJson(4000, '请选择是否工程挂账');
                }
                if($param['isgua'] == '是'){
                    if(empty($param['enginid'])){
                        return outputJson(4000, '请选择工程名称');
                    }
                }
                if(empty($param['jidt'])){
                    return outputJson(4000, '请选择收件日期');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                KuaiShouModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('kuai_shou', $param['id']);
                }else{
                    (new FlowService())->save_bill('kuai_shou', $param['id']);
                }

                (new OptService())->relation_file($files,'kuai_shou',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'kuai_shou', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'kuai_shou', $param['id'], '快递收件');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = KuaiShouModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

            $data['jier_info'] = (new UserService())->get_users_part_arr($data['data']['jier'], 'user');


            $data['files'] = (new OptService())->get_file('kuai_shou', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('kuai_shou');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('kuai_shou',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('kuai_shou');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function add_car_jia(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['carid'])){
                    return outputJson(4000, '请选择车牌号');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择加油日期');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择类型');
                }
                if(empty($param['sheng'])){
                    return outputJson(4000, '请输入加油升数');
                }
                if(empty($param['sheng'])){
                    return outputJson(4000, '请输入加油金额');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = CarJiaModel::insertGetId($param);

                (new FlowService())->save_bill('car_jia', $id);

                (new OptService())->relation_file($files,'car_jia',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'car_jia', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'car_jia', $id, '加油登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['type'] = (new XingService())->get_car_xi_type();
            $data['baner'] = $this->userinfo['realname'];
            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('car_jia');
            $data['flowsetname'] = FlowSetModel::get_name('car_jia');
            $data['isadvan'] = (new FlowService())->check_isadvan('car_jia');
            $data['isnext'] = (new FlowService())->check_isnext('car_jia');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_car_jia(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['carid'])){
                    return outputJson(4000, '请选择车牌号');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择加油日期');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择类型');
                }
                if(empty($param['sheng'])){
                    return outputJson(4000, '请输入加油升数');
                }
                if(empty($param['sheng'])){
                    return outputJson(4000, '请输入加油金额');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                CarJiaModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('car_jia', $param['id']);
                }else{
                    (new FlowService())->save_bill('car_jia', $param['id']);
                }

                (new OptService())->relation_file($files,'car_jia',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'car_jia', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'car_jia', $param['id'], '加油登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CarJiaModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['type'] = (new XingService())->get_car_xi_type();
            $data['carinfo'] = CarModel::get_info_by_pk($data['data']['carid']);
            $data['data']['jiayufee'] = (new XingService())->get_car_jiayufee($data['data']['carid']);

            $data['files'] = (new OptService())->get_file('car_jia', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('car_jia');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('car_jia',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('car_jia');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_car_xi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['carid'])){
                    return outputJson(4000, '请选择车牌号');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择日期');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择类型');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }

                if($param['type'] == '消费'){
                    $shengArr = (new XingService())->get_car_xi_shengci();
                    if($param['benpu'] > $shengArr['shengpu']){
                        return outputJson(4000, '本次普洗次数大于剩余次数,请处理后提交');
                    }
                    if($param['benjing'] > $shengArr['shengjing']){
                        return outputJson(4000, '本次精洗次数大于剩余次数,请处理后提交');
                    }
                    if($param['benla'] > $shengArr['shengla']){
                        return outputJson(4000, '本次打蜡次数大于剩余次数,请处理后提交');
                    }
                    if($param['benqi'] > $shengArr['shengqi']){
                        return outputJson(4000, '本次其他次数大于剩余次数,请处理后提交');
                    }
                }

            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = CarXiModel::insertGetId($param);

                (new FlowService())->save_bill('car_xi', $id);

                (new OptService())->relation_file($files,'car_xi',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'car_xi', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'car_xi', $id, '洗车登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['baner'] = $this->userinfo['realname'];
            $data['type'] = (new XingService())->get_car_xi_type();
            $data['shengArr'] = (new XingService())->get_car_xi_shengci();

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('car_xi');
            $data['flowsetname'] = FlowSetModel::get_name('car_xi');
            $data['isadvan'] = (new FlowService())->check_isadvan('car_xi');
            $data['isnext'] = (new FlowService())->check_isnext('car_xi');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_car_xi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['carid'])){
                    return outputJson(4000, '请选择车牌号');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择日期');
                }
                if(empty($param['type'])){
                    return outputJson(4000, '请选择类型');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }

                if($param['type'] == '消费'){
                    $shengArr = (new XingService())->get_car_xi_shengci();
                    if($param['benpu'] > $shengArr['shengpu']){
                        return outputJson(4000, '本次普洗次数大于剩余次数,请处理后提交');
                    }
                    if($param['benjing'] > $shengArr['shengjing']){
                        return outputJson(4000, '本次精洗次数大于剩余次数,请处理后提交');
                    }
                    if($param['benla'] > $shengArr['shengla']){
                        return outputJson(4000, '本次打蜡次数大于剩余次数,请处理后提交');
                    }
                    if($param['benqi'] > $shengArr['shengqi']){
                        return outputJson(4000, '本次其他次数大于剩余次数,请处理后提交');
                    }
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                CarXiModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('car_xi', $param['id']);
                }else{
                    (new FlowService())->save_bill('car_xi', $param['id']);
                }

                (new OptService())->relation_file($files,'car_xi',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'car_xi', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'car_xi', $param['id'], '洗车登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CarXiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['carinfo'] = CarModel::get_info_by_pk($data['data']['carid']);

            $data['baner'] = $this->userinfo['realname'];
            $data['type'] = (new XingService())->get_car_xi_type();
            $data['data']['shengArr'] = (new XingService())->get_car_xi_shengci();

            $data['files'] = (new OptService())->get_file('car_xi', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('car_xi');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('car_xi',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('car_xi');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function add_car_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['carid'])){
                    return outputJson(4000, '请选择车牌号');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择保养日期');
                }
                if(empty($param['baoxiang'])){
                    return outputJson(4000, '请选择保养项目');
                }
                if(empty($param['baofee'])){
                    return outputJson(4000, '请输入保养费用');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }
                if(empty($param['dangshu'])){
                    return outputJson(4000, '请输入当前公里数');
                }
                if(empty($param['xiashu'])){
                    return outputJson(4000, '请输入下次保养公里数');
                }
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['flowuser'] = $this->userinfo['realname'];
            $param['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = CarBaoModel::insertGetId($param);

                (new FlowService())->save_bill('car_bao', $id);

                (new OptService())->relation_file($files,'car_bao',$id);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'car_bao', $id);
                    (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'car_bao', $id, '保养登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'), ['billid'=>$id]);
        }else{
            $data['baner'] = $this->userinfo['realname'];
            $data['baoxiang'] = (new OptService())->get_base_info('carbao_baoxiang');

            $data['flowuser'] = $this->userinfo['realname'];
            $data['files'] = (new OptService())->get_file('car_bao');
            $data['flowsetname'] = FlowSetModel::get_name('car_bao');
            $data['isadvan'] = (new FlowService())->check_isadvan('car_bao');
            $data['isnext'] = (new FlowService())->check_isnext('car_bao');
            $data['flowdept'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_car_bao(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if($param['isturn'] > 0){
                if(empty($param['carid'])){
                    return outputJson(4000, '请选择车牌号');
                }
                if(empty($param['dt'])){
                    return outputJson(4000, '请选择保养日期');
                }
                if(empty($param['baoxiang'])){
                    return outputJson(4000, '请选择保养项目');
                }
                if(empty($param['baofee'])){
                    return outputJson(4000, '请输入保养费用');
                }
                if(empty($param['baner'])){
                    return outputJson(4000, '请输入经手人');
                }
                if(empty($param['dangshu'])){
                    return outputJson(4000, '请输入当前公里数');
                }
                if(empty($param['xiashu'])){
                    return outputJson(4000, '请输入下次保养公里数');
                }
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            if($param['isturn'] == 0){
                $param['created_at'] = date('Y-m-d H:i:s');
            }
            $files = array_filter($param['files']);
            unset($param['files']);
            $isshen = $param['isshen'];
            unset($param['isshen']);
            unset($param['token']);

            Db::startTrans();
            try {
                CarBaoModel::update($param);

                if($isshen > 0){
                    (new FlowService())->save_bill_shen('car_bao', $param['id']);
                }else{
                    (new FlowService())->save_bill('car_bao', $param['id']);
                }

                (new OptService())->relation_file($files,'car_bao',$param['id']);
                if($param['isturn'] > 0){
                    (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'car_bao', $param['id']);
                    (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'car_bao', $param['id'], '保养登记');
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = CarBaoModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['carinfo'] = CarModel::get_info_by_pk($data['data']['carid']);

            $data['baner'] = $this->userinfo['realname'];
            $data['baoxiang'] = (new OptService())->get_base_info('carbao_baoxiang');

            $data['files'] = (new OptService())->get_file('car_bao', $data['data']['id']);
            $data['data']['flowrelas_info'] = (new UserService())->get_flowrelas_info($data['data']['flowrelas']);
            $data['data']['advaners_info'] = (new UserService())->get_users_part_arr($data['data']['advaners'], 'user');
            $data['data']['nexter_info'] = (new UserService())->get_users_part_arr($data['data']['nexter'], 'user');
            $data['data']['flowsetname'] = FlowSetModel::get_name('car_bao');
            $data['data']['isadvan'] = (new FlowService())->check_isadvan_edit('car_bao',$data['data']);
            $data['data']['isnext'] = (new FlowService())->check_isnext('car_bao');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }




































}
